home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / 2004-12 CHIP.iso / CHIP / Porady / Srodowisko PHP-MySQL / ACTIVESTATE PERL ADD-ON / PERL_add-on.exe / {app} / perl / bin / ppm3-bin < prev    next >
Text File  |  2004-06-01  |  146KB  |  4,889 lines

  1. #!perl 
  2.  
  3. require 5.006;    # require 5.6.0
  4. use strict;
  5.  
  6. # A command-line shell implementation. The code which invokes it is at the
  7. # bottom of this file.
  8. package PPMShell;
  9. use base qw(PPM::Term::Shell);
  10.  
  11. use Data::Dumper;
  12. use Text::Autoformat qw(autoformat form);
  13. use Getopt::Long;
  14.  
  15. # These must come _after_ the options parsing.
  16. require PPM::UI;
  17. require PPM::Trace;
  18. PPM::Trace->import(qw(trace));
  19.  
  20. my $NAME    = q{PPM - Programmer's Package Manager};
  21. my $SHORT_NAME    = q{PPM};
  22. our $VERSION    = '3.1';
  23.  
  24. sub dictsort(@);
  25.  
  26. #=============================================================================
  27. # Output Methods
  28. #
  29. # PPM behaves differently under different calling circumstances. Here are the
  30. # various classes of messages it prints out:
  31. # 1. error/warning    - an error or "bad thing" has occurred
  32. # 2. informational    - required information like search results
  33. # 3. verbose        - verbose that's only needed in interactive mode
  34. #
  35. # Here are the cases:
  36. # 1. PPM is in interactive mode: everything gets printed.
  37. # 2. PPM is in batch mode: everything minus 'verbose' gets printed.
  38. #=============================================================================
  39. sub error {
  40.     my $o = shift;
  41.     return 1 unless $o->{SHELL}{output}{error};
  42.     CORE::print STDERR @_;
  43. }
  44. sub errorf {
  45.     my $o = shift;
  46.     return 1 unless $o->{SHELL}{output}{error};
  47.     CORE::printf STDERR @_;
  48. }
  49. sub warn { goto &error }
  50. sub warnf { goto &errorf }
  51. sub inform {
  52.     my $o = shift;
  53.     return 1 unless $o->{SHELL}{output}{inform};
  54.     CORE::print @_;
  55. }
  56. sub informf {
  57.     my $o = shift;
  58.     return 1 unless $o->{SHELL}{output}{inform};
  59.     CORE::printf @_;
  60. }
  61. sub verbose {
  62.     my $o = shift;
  63.     return 1 unless $o->{SHELL}{output}{verbose};
  64.     CORE::print @_;
  65. }
  66. sub verbosef {
  67.     my $o = shift;
  68.     return 1 unless $o->{SHELL}{output}{verbose};
  69.     CORE::printf @_;
  70. }
  71. sub assertw {
  72.     my $o = shift;
  73.     my $cond = shift;
  74.     my $msg = shift;
  75.     $o->warn("Warning: $msg\n") unless $cond;
  76.     return $cond;
  77. }
  78. sub assert {
  79.     my $o = shift;
  80.     my $cond = shift;
  81.     my $msg = shift;
  82.     $o->error("Error: $msg\n") unless $cond;
  83.     return $cond;
  84. }
  85.  
  86. sub mode {
  87.     my $o = shift;
  88.     $o->{SHELL}{mode};
  89. }
  90. sub setmode {
  91.     my $o = shift;
  92.     my $newmode = shift || '';
  93.     my $oldmode = $o->{SHELL}{mode};
  94.     if ($newmode eq 'SHELL') {
  95.     $o->{SHELL}{output}{error}   = 1;
  96.     $o->{SHELL}{output}{inform}  = 1;
  97.     $o->{SHELL}{output}{verbose} = 1;
  98.     }
  99.     elsif ($newmode eq 'BATCH') {
  100.     $o->{SHELL}{output}{error}   = 1;
  101.     $o->{SHELL}{output}{inform}  = 1;
  102.     $o->{SHELL}{output}{verbose} = 0;
  103.     }
  104.     elsif ($newmode eq 'SCRIPT') {
  105.     $o->{SHELL}{output}{error}   = 1;
  106.     $o->{SHELL}{output}{inform}  = 1;
  107.     $o->{SHELL}{output}{verbose} = 0;
  108.     }
  109.     elsif ($newmode eq 'SILENT') {
  110.     $o->{SHELL}{output}{error}   = 1;
  111.     $o->{SHELL}{output}{inform}  = 0;
  112.     $o->{SHELL}{output}{verbose} = 0;
  113.     }
  114.     $o->{SHELL}{mode} = $newmode;
  115.     return $oldmode;
  116. }
  117.  
  118. # Older versions of PPM3 had one "Active" repository. This code reads
  119. # $o->conf('repository') if it exists, and moves it into
  120. # $o->conf('active_reps'), which is a list. The old one is deleted -- old PPMs
  121. # will reset it if needed, but it will be ignored if 'active_reps' exists.
  122. sub init_active_reps {
  123.     my $o = shift;
  124.  
  125.     if ($o->conf('repository') and not $o->conf('active_reps')) {
  126.     my @active = $o->conf('repository');
  127.     delete $o->{SHELL}{conf}{DATA}{repository};
  128.     $o->conf('active_reps', \@active);
  129.     }
  130.     elsif (not defined $o->conf('active_reps')) {
  131.     my @active = $o->reps_all; # enable all repositories
  132.     $o->conf('active_reps', \@active);
  133.     }
  134. }
  135.  
  136. sub init {
  137.     my $o = shift;
  138.     $o->cache_clear('query');
  139.     $o->cache_clear('search');
  140.     $o->{API}{case_ignore} = 1;
  141.  
  142.     # Load the configuration;
  143.     $o->{SHELL}{conf} = PPM::Config::load_config_file('cmdline');
  144.     $o->init_active_reps;
  145.  
  146.     # check whether there's a target in the parent's perl that hasn't been
  147.     # installed in the "targets" file:
  148.     my $ppmsitelib = $ENV{PPM3_PERL_SITELIB};
  149.     if ($ppmsitelib and opendir(PPMDIR, "$ppmsitelib/ppm-conf")) {
  150.         my @files = map  { "$ppmsitelib/ppm-conf/$_" }
  151.                 grep { /^ppminst/i && !/(~|\.bak)\z/ } readdir PPMDIR;
  152.     closedir PPMDIR;
  153.     my $found = 0;
  154.     if (@files == 1) {
  155.         my @targets = PPM::UI::target_list()->result_l;
  156.         for my $target (@targets) {
  157.         my $info = PPM::UI::target_raw_info($target);
  158.         next unless $info and $info->is_success;
  159.         ++$found and last
  160.             if path_under($info->result->{path}, $files[0]);
  161.         }
  162.         unless ($found) {
  163.         # We're going to add a new target:
  164.         # 1. if we can find ppm3-bin.cfg, use that
  165.         # 2. if not, guess lots of stuff
  166.         my $ppm3_bin_cfg = "$ENV{PPM3_PERL_PREFIX}/bin/ppm3-bin.cfg";
  167.         my $r = PPM::UI::target_add(undef, From => $ppm3_bin_cfg)
  168.             if -f $ppm3_bin_cfg;
  169.         unless ($r and $r->is_success) {
  170.             PPM::UI::target_add(
  171.             'TEMP',
  172.             type => 'Local',
  173.             path => $files[0],
  174.             );
  175.         }
  176.         }
  177.     }
  178.     }
  179.  
  180.     # set the initial target:
  181.     if (defined $o->{API}{args}{target}) {
  182.     my $t = $o->{API}{args}{target};
  183.     my $prefix = $ENV{PPM3_PERL_PREFIX};
  184.     if ($t ne 'auto') {
  185.         # A full name or number given:
  186.         $o->run('target', 'select', $o->{API}{args}{target});
  187.     }
  188.     elsif ($prefix) {
  189.         # Auto-select target, based on where we came from:
  190.         my @l = $o->conf('target');
  191.         push @l, PPM::UI::target_list()->result_l;
  192.         for my $target (@l) {
  193.         next unless $target;
  194.         my $info = PPM::UI::target_raw_info($target);
  195.         next unless $info and $info->is_success;
  196.         next unless path_under($info->result->{path}, "$prefix/");
  197.         my $mode = $o->setmode('SILENT');
  198.         $o->run('target', 'select', $target);
  199.         $o->setmode($mode);
  200.         last;
  201.         }
  202.     }
  203.     }
  204. }
  205.  
  206. sub preloop {
  207.     my $o = shift;
  208.  
  209.     if ($o->conf('verbose-startup') and $o->mode eq 'SHELL') {
  210.     my $profile_track = $o->conf('profile-track');
  211.     chomp (my $startup = <<END);
  212. $NAME version $VERSION.
  213. Copyright (c) 2001 ActiveState Corp. All Rights Reserved.
  214. ActiveState is a devision of Sophos.
  215.  
  216. Entering interactive shell.
  217. END
  218.  
  219.     my $file = PPM::Config::get_license_file();
  220.     my $license;
  221.     if (open (my $LICENSE, $file)) {
  222.         $license = do { local $/; <$LICENSE> };
  223.     }
  224.     my $aspn = $license =~ /ASPN/;
  225.     my $profile_tracking_warning = ($profile_track || !$aspn) ? '' : <<'END';
  226.  
  227. Profile tracking is not enabled. If you save and restore profiles manually,
  228. your profile may be out of sync with your computer. See 'help profile' for
  229. more information.
  230. END
  231.     $o->inform($startup);
  232.     $o->inform(<<END);
  233.  Using $o->{API}{readline} as readline library.
  234. $profile_tracking_warning
  235. Type 'help' to get started.
  236.  
  237. END
  238.     }
  239.     else {
  240.     $o->inform("$NAME ($VERSION). Type 'help' to get started.\n");
  241.     }
  242.  
  243.     $o->term->SetHistory(@{$o->conf('history') || []})
  244.     if $o->term->Features->{setHistory};
  245. }
  246.  
  247. sub postloop {
  248.     my $o = shift;
  249.     trace(1, "PPM: exiting...\n");
  250.     if ($o->mode eq 'SHELL' and $o->term->Features->{getHistory}) {
  251.     my @h = $o->term->GetHistory;
  252.     my $max_history = $o->conf('max_history') || 100;
  253.     splice @h, 0, (@h - $max_history)
  254.         if @h > $max_history;
  255.     my $old = $o->setmode('SILENT');
  256.     $o->conf('history', \@h);
  257.     $o->setmode($old);
  258.     }
  259. }
  260.  
  261. #============================================================================
  262. # Cache of search and query results
  263. #============================================================================
  264. sub cache_set_current {
  265.     my $o = shift;
  266.     my $type = shift;
  267.     my $set = shift;
  268.     $set = $o->{SHELL}{CACHE}{$type}{current} unless defined $set;
  269.     $o->{SHELL}{CACHE}{$type}{current} = $set;
  270.     return $o->{SHELL}{CACHE}{$type}{current};
  271. }
  272.  
  273. sub cache_set_index {
  274.     my $o = shift;
  275.     my $type = shift;
  276.     my $index = shift;
  277.     $index = $o->{SHELL}{CACHE}{$type}{index} unless defined $index;
  278.     $o->{SHELL}{CACHE}{$type}{index} = $index;
  279.     return $o->{SHELL}{CACHE}{$type}{index};
  280. }
  281.  
  282. sub cache_set_add {
  283.     my $o = shift;
  284.     my $type = shift;
  285.     my $query = shift;
  286.     my $entries = shift;
  287.     my $sort_field = $o->conf('sort-field');
  288.     my @sorted = $o->sort_pkgs($sort_field, @$entries);
  289.     my $set = {
  290.           query => $query,
  291.           raw => $entries,
  292.           $sort_field => \@sorted,
  293.         };
  294.     push @{$o->{SHELL}{CACHE}{$type}{sets}}, $set;
  295. }
  296.  
  297. sub cache_entry {
  298.     my $o = shift;
  299.     my $type = shift;        # 'query' or 'cache';
  300.     my $index = shift;        # defaults to currently selected index
  301.     my $set = shift;        # defaults to currently selected set
  302.  
  303.     $index = $o->{SHELL}{CACHE}{$type}{index} unless defined $index;
  304.  
  305.     my $src = $o->cache_set($type, $set);
  306.     return undef unless $src and bounded(0, $index, $#$src);
  307.  
  308.     # Make sure we display only valid entries:
  309.     my $tar = $o->conf('target');
  310.     $src->[$index]->make_complete($tar);
  311.     return $src->[$index];
  312. }
  313.  
  314. sub cache_set {
  315.     my $o = shift;
  316.     my $type = shift;        # 'query' or 'cache'
  317.     my $set = shift;        # defaults to currently selected set
  318.     my $entry = shift;        # defaults to 'results';
  319.  
  320.     $entry = $o->conf('sort-field') unless defined $entry;
  321.     return undef unless grep { lc($entry) eq $_ } (sort_fields(), 'query');
  322.  
  323.     $set = $o->{SHELL}{CACHE}{$type}{current} unless defined $set;
  324.     my $src = $o->{SHELL}{CACHE}{$type}{sets};
  325.  
  326.     return undef unless defined $set;
  327.     return undef unless bounded(0, $set, $#$src);
  328.  
  329.     # We've changed sort-field at some point -- make sure the sorted data
  330.     # exists, or else build it:
  331.     unless (defined $src->[$set]{$entry}) {
  332.     my $raw = $src->[$set]{raw};
  333.     my @sorted = $o->sort_pkgs($entry, @$raw);
  334.     $src->[$set]{$entry} = \@sorted;
  335.     }
  336.     
  337.     return wantarray ? @{$src->[$set]{$entry}} : $src->[$set]{$entry};
  338. }
  339.  
  340. sub cache_clear {
  341.     my $o = shift;
  342.     my $type = shift;        # 'query' or 'cache'
  343.     $o->{SHELL}{CACHE}{$type}{sets} = [];
  344.     $o->{SHELL}{CACHE}{$type}{current} = -1;
  345.     $o->{SHELL}{CACHE}{$type}{index} = -1;
  346. }
  347.  
  348. sub cache_sets {
  349.     my $o = shift;
  350.     my $type = shift;
  351.     @{$o->{SHELL}{CACHE}{$type}{sets}};
  352. }
  353.  
  354. # This sub searches for an entry in the cache whose name matches that thing
  355. # passed in. It searches in the current cache first. If the name isn't found,
  356. # it searches in all caches. If the name still isn't found, it returns undef.
  357. sub cache_find {
  358.     my $o = shift;
  359.     my $type = shift;
  360.     my $name = shift;
  361.  
  362.     my $ncaches = $o->cache_sets($type);
  363.     my $current = $o->cache_set_current($type);
  364.  
  365.     # First, search the current set:
  366.     my @pkgs = map { $_ ? $_->name : '' } $o->cache_set($type);
  367.     my $ind  = find_index($name, 0, @pkgs);
  368.     return ($current, $ind) if $ind >= 0;
  369.  
  370.     # Now try to find in all the sets:
  371.     for my $s (0 .. $ncaches - 1) {
  372.     next if $s == $current;
  373.     @pkgs = map { $_ ? $_->name : '' } $o->cache_set($type, $s);
  374.     $ind  = find_index($name, 0, @pkgs);
  375.     return ($s, $ind) if $ind >= 0;
  376.     }
  377.     return (-1, -1);
  378. }
  379.  
  380. # A pretty separator to print between logically separate items:
  381. my $SEP;
  382. BEGIN {
  383.     $SEP = '=' x 20;
  384. }
  385.  
  386. # Useful functions:
  387. sub max (&@) {
  388.     my $code = shift;
  389.     my $max;
  390.     local $_;
  391.     for (@_) {
  392.     my $res = $code->($_);
  393.     $max = $res if not defined $max or $max < $res;
  394.     }
  395.     $max || 0;
  396. }
  397.  
  398. sub min (&@) {
  399.     my $code = shift;
  400.     my $min;
  401.     local $_;
  402.     for (@_) {
  403.     my $res = $code->($_);
  404.     $min = $res if not defined $min or $min > $res;
  405.     }
  406.     $min || 0;
  407. }
  408.  
  409. sub sum (&@) {
  410.     my $code = shift;
  411.     my $sum = 0;
  412.     local $_;
  413.     for (@_) {
  414.     my $res = $code->($_);
  415.     $sum += $res if defined $res;
  416.     }
  417.     $sum || 0;
  418. }
  419.  
  420. #============================================================================
  421. # Repository:
  422. # rep            # displays repositories
  423. # rep add http://...    # adds a new repository
  424. # rep del <\d+>        # deletes the specified repository
  425. # rep [set] 1        # sets the specified repository active
  426. #============================================================================
  427. sub smry_repository { "adds, removes, or sets repositories" }
  428. sub help_repository { <<'END' }
  429. repository -- Repository Control
  430.   Synopsis
  431.      rep                        Displays all repositories
  432.      rep add [name] <location>  Adds a new repository; makes it active
  433.      rep delete <name or num>   Deletes specified repository
  434.      rep describe <name or num> Displays information about the specified
  435.                                 repository
  436.      rep rename <name or num> <name>
  437.                                 Renames the specified repository to
  438.                                 the given name
  439.      rep on <name>              Activates the specified repository
  440.      rep off <name or num>      Removes the repository from the active list
  441.      rep up <name or num>       Moves the specified repository up one
  442.      rep down <name or num>     Moves the specified repository down one
  443.  
  444.     The <name> needs to be put inside doublequotes if it contains any
  445.     spaces.
  446.  
  447.   Description
  448.     The *repository* (or *rep*) command controls two lists or repositories:
  449.  
  450.     1   The list of "active" repositories. This is the list of repositories
  451.         used by *search*, *install*, and *upgrade*.
  452.  
  453.     2   The list of all known repositories. You can designate a repository
  454.         "inactive", which means PPM will not use it in any commands.
  455.  
  456.     If no arguments are given, the rep command will list the active
  457.     repositories defined in the PPM settings. The order is significant: when
  458.     installing a package, PPM will try the first repository, then the
  459.     second, and so on, until it find the package you asked for. When
  460.     searching, PPM merges the results of all the repositories together, so
  461.     the order is less important (see the *search* command).
  462.  
  463.     For example, when you enter:
  464.  
  465.         rep
  466.  
  467.     PPM3 will return something resembling this:
  468.  
  469.         Repositories:
  470.         [1] ActiveCD
  471.         [2] ActiveState Package Repository
  472.         [ ] An inactive repository
  473.  
  474.     In the example above, entering 'rep off 2' will disable the second
  475.     repository (the ActiveStat Package Repository). To add another
  476.     repository:
  477.  
  478.         rep add [options] <NAME> <LOCATION>
  479.  
  480.     The following options are available for the 'add' command:
  481.  
  482.     *   -username
  483.  
  484.     *   -password
  485.  
  486.     These options allow you to specify a username and password to be used
  487.     when logging into a repository. Currently, these are only used for FTP
  488.     and WWW repositories.
  489.  
  490.     For example:
  491.  
  492.         rep add "EZE" http://foo.com/MyPPMPackages
  493.  
  494.     with "EZE" being the name of the repository (for easy reference) and the
  495.     location noted by the http location. If you were to enter the rep
  496.     command again, you would see:
  497.  
  498.         ppm> rep
  499.         Repositories:
  500.         [1] ActiveCD
  501.         [2] ActiveState Package Repository
  502.         [3] EZE
  503.         [ ] An inactive repository
  504.  
  505.     To move the new repository to the top of the Active list, you would
  506.     type:
  507.  
  508.         ppm> rep up EZE
  509.         Repositories:
  510.         [1] ActiveCD
  511.         [2] EZE
  512.         [3] ActiveState Package Repository
  513.         [ ] An inactive repository
  514.         ppm> rep up EZE
  515.         Repositories:
  516.         [1] EZE
  517.         [2] ActiveCD
  518.         [3] ActiveState Package Repository
  519.         [ ] An inactive repository
  520.  
  521.     To disable the ActiveCD repository temporarily, enter the following:
  522.  
  523.         ppm> rep off ActiveCD
  524.         Repositories:
  525.         [1] EZE
  526.         [2] ActiveState Package Repository
  527.         [ ] ActiveCD
  528.         [ ] An inactive repository
  529.  
  530.     To describe a repository, refer to it either by name, or by the number
  531.     displayed next to the repository in the Active Repositories list. You
  532.     must refer to inactive repositories by their full name.
  533.  
  534.         ppm> rep describe 2
  535.         Describing Active Repository 2:
  536.             Name: ActiveState Package Repository
  537.         Location: http://ppm.ActiveState.com/cgibin/PPM/...
  538.             Type: PPMServer 2.00
  539.         ppm> rep describe ActiveCD
  540.         Describing Inactive Repository:
  541.             Name: ActiveCD
  542.         Location: F:\PPMPackages\5.8plus
  543.             Type: Local Directory
  544.  
  545.     To re-activate the ActiveCD repository, use the *rep on* command. You
  546.     must refer to inactive repositories by name, not number.
  547.  
  548.         ppm> rep on ActiveCD
  549.         Active Repositories:
  550.         [1] EZE
  551.         [2] ActiveState Package Repository
  552.         [3] ActiveCD
  553.         [ ] An inactive repository
  554.  
  555.   Repository Types
  556.     PPM3 supports several types of package repositories:
  557.  
  558.     1.  PPM Server 3
  559.  
  560.         ActiveState's SOAP-driven package server. Because all searches are
  561.         done server-side, the server can deliver much richer information
  562.         about packages than other repositories.
  563.  
  564.     2.  PPM Server 2
  565.  
  566.         The SOAP server designed for PPM version 2. PPM 3.1 ships with the
  567.         PPM2 repository as well as the PPM3 repository, so you can use
  568.         either. Simple searches are performed server-side. If your search is
  569.         too complicated for the server, PPM 3.1 will download the package
  570.         summary and search by itself.
  571.  
  572.     3.  Web Repositories
  573.  
  574.         Older versions of PPM used non-SOAP repositories (directories full
  575.         of PPD files accessible using a web browser). Over the history of
  576.         PPM, there have been several different ways of organising the files
  577.         so that PPM can search for packages properly. PPM3 tries to download
  578.         a summary file first -- if that fails, it gets the directory index.
  579.         It parses the summary or the index, and caches it. Searches are done
  580.         from the cache.
  581.  
  582.     4.  FTP Repositories
  583.  
  584.         FTP is another way of exposing a directory full of PPD files. PPM3
  585.         consideres FTP repositories a subset of Web repositories. Treat them
  586.         as identical: PPM3 downloads the summary or the "index" (file
  587.         listing in this case), parses it, and then searches from it.
  588.  
  589.     5.  Local Repositories
  590.  
  591.         To support installing packages from the ActiveCD, a local directory
  592.         can be a repository. PPM searches the files in the directory. All
  593.         valid path formats are supported, including UNC paths.
  594. END
  595. sub comp_repository {
  596.     my $o = shift;
  597.     my ($word, $line, $start) = @_;
  598.     my @words = $o->line_parsed($line);
  599.     my $words = scalar @words;
  600.     my @reps = PPM::UI::repository_list()->result_l;
  601.     my $reps = @reps;
  602.     my @compls = qw(add delete describe rename set select);
  603.     push @compls, ($reps ? (1 .. $reps) : ()); 
  604.  
  605.     if ($words == 1 or $words == 2 and $start != length($line)) {
  606.     return $o->completions($word, \@compls);
  607.     }
  608.     if ($words == 2 or $words == 3 and $start != length($line)) {
  609.     return (readline::rl_filename_list($word))
  610.       if $words[1] eq 'add';
  611.     return $o->completions($word, [1 .. $reps])
  612.       if $o->completions($words[1], [qw(delete describe rename set select)]) == 1;
  613.     }
  614.     ();
  615. }
  616. sub reps_all {
  617.     my $o = shift;
  618.     my $l = PPM::UI::repository_list();
  619.     unless ($l->is_success) {
  620.     $o->warn($l->msg);
  621.     return () unless $l->ok;
  622.     }
  623.     $l->result_l;
  624. }
  625. sub reps_on {
  626.     my $o = shift;
  627.     return @{$o->conf('active_reps')};
  628. }
  629. sub reps_off {
  630.     my $o = shift;
  631.     my @reps = $o->reps_all;
  632.     my @reps_on = $o->reps_on;
  633.     my @off;
  634.     for my $r (@reps) {
  635.     push @off, $r unless grep { $_ eq $r } @reps_on;
  636.     }
  637.     @off;
  638. }
  639. sub rep_on {
  640.     my $o = shift;
  641.     my $rep = shift;
  642.     my @reps = ($o->reps_on, $rep);
  643.     my $m = $o->setmode('SILENT');
  644.     $o->conf('active_reps', \@reps);
  645.     $o->setmode($m);
  646. }
  647. sub rep_off {
  648.     my $o = shift;
  649.     my $rep = shift;
  650.     my @reps = grep { $_ ne $rep } $o->reps_on;
  651.     my $m = $o->setmode('SILENT');
  652.     $o->conf('active_reps', \@reps);
  653.     $o->setmode($m);
  654. }
  655. sub rep_ison {
  656.     my $o = shift;
  657.     my $rep = shift;
  658.     scalar grep { $_ eq $rep } $o->reps_on;
  659. }
  660. sub rep_isoff {
  661.     my $o = shift;
  662.     my $rep = shift;
  663.     scalar grep { $_ eq $rep } $o->reps_off;
  664. }
  665. sub rep_exists {
  666.     my $o = shift;
  667.     my $rep = shift;
  668.     scalar grep { $_ eq $rep } $o->reps_all;
  669. }
  670. sub rep_uniq {
  671.     my $o = shift;
  672.     my $rep = shift;
  673.     unless ($o->rep_exists($rep) or $rep =~ /^\d+$/) {
  674.     /\Q$rep\E/i and return $_ for $o->reps_all;
  675.     }
  676.     $rep;
  677. }
  678. sub rep_up {
  679.     my $o = shift;
  680.     my $rep = shift;
  681.     my @reps = $o->reps_on;
  682.     my $ind = find_index($rep, 0, @reps);
  683.     if (bounded(1, $ind, $#reps)) {
  684.     @reps = (
  685.         @reps[0 .. $ind - 2],
  686.         $rep,
  687.         $reps[$ind - 1],
  688.         @reps[$ind + 1 .. $#reps]
  689.     );
  690.     }
  691.     my $m = $o->setmode('SILENT');
  692.     $o->conf('active_reps', \@reps);
  693.     $o->setmode($m);
  694. }
  695. sub rep_down {
  696.     my $o = shift;
  697.     my $rep = shift;
  698.     my @reps = $o->reps_on;
  699.     my $ind = find_index($rep, 0, @reps);
  700.     if (bounded(0, $ind, $#reps - 1)) {
  701.     @reps = (
  702.         @reps[0 .. $ind - 1],
  703.         $reps[$ind + 1],
  704.         $rep,
  705.         @reps[$ind + 2 .. $#reps]
  706.     );
  707.     }
  708.     my $m = $o->setmode('SILENT');
  709.     $o->conf('active_reps', \@reps);
  710.     $o->setmode($m);
  711. }
  712. sub run_repository {
  713.     my $o = shift;
  714.     my @args = @_;
  715.     my (@reps, @reps_off, @reps_on);
  716.     my $refresh = sub {
  717.     @reps = $o->reps_all;
  718.     @reps_off = $o->reps_off;
  719.     @reps_on = $o->reps_on;
  720.     };
  721.     &$refresh;
  722.     trace(1, "PPM: repository @args\n");
  723.  
  724.     if (@args) {
  725.     my $cmd = shift @args;
  726.     #=====================================================================
  727.     # add, delete, describe, rename commands:
  728.     #=====================================================================
  729.     if (matches($cmd, "add")) {
  730.         # Support for usernames and passwords.
  731.         my ($user, $pass);
  732.         {
  733.         local *ARGV;
  734.         @ARGV = @args;
  735.         GetOptions(
  736.             "username=s"    => \$user,
  737.             "password=s"    => \$pass,
  738.         );
  739.         @args = @ARGV;
  740.         }
  741.         $o->warn(<<END) and return unless @args;
  742. repository: invalid 'add' command arguments. See 'help repository'.
  743. END
  744.         my $url  = pop @args;
  745.         my $name = join(' ', @args);
  746.         unless ($name) {    # rep add http://...
  747.         $name = 'Autonamed';
  748.         for (my $i=1; $i<=@reps; $i++) {
  749.             my $tmp = "$name $i";
  750.             $name = $tmp and last
  751.               unless (grep { $tmp eq $_ } @reps);
  752.         }
  753.         }
  754.         my $ok = PPM::UI::repository_add($name, $url, $user, $pass);
  755.         unless ($ok->is_success) {
  756.         $o->warn($ok->msg);
  757.         return unless $ok->ok;
  758.         }
  759.         $o->rep_on($name);
  760.         $o->cache_clear('search');
  761.     }
  762.     elsif (matches($cmd, "del|ete")) {
  763.         my $arg = join(' ', @args);
  764.         my $gonner = $arg;
  765.         if ($arg =~ /^\d+$/) {
  766.         return unless $o->assert(
  767.             bounded(1, $arg, scalar @reps_on),
  768.             "no such active repository $arg"
  769.         );
  770.         $gonner = $reps_on[$arg - 1];
  771.         }
  772.         else {
  773.         $gonner = $o->rep_uniq($gonner);
  774.         return unless $o->assert(
  775.             $o->rep_exists($gonner),
  776.             "no such repository '$gonner'"
  777.         );
  778.         }
  779.         my $ok = PPM::UI::repository_del($gonner);
  780.         unless ($ok->is_success) {
  781.         $o->warn($ok->msg);
  782.         return unless $ok->ok;
  783.         }
  784.         $o->rep_off($gonner);
  785.         $o->cache_clear('search');
  786.     }
  787.     elsif (matches($cmd, "des|cribe")) {
  788.         my $arg = join(' ', @args) || 1;
  789.         my $rep = $arg;
  790.         if ($arg =~ /^\d+$/) {
  791.         return unless $o->assert(
  792.             bounded(1, $arg, scalar @reps_on),
  793.             "no such active repository $arg"
  794.         );
  795.         $rep = $reps_on[$arg - 1];
  796.         }
  797.         else {
  798.         $rep = $o->rep_uniq($rep);
  799.         return unless $o->assert(
  800.             $o->rep_exists($rep),
  801.             "no such repository '$rep'"
  802.         );
  803.         }
  804.         my $info = PPM::UI::repository_info($rep);
  805.         unless ($info->is_success) {
  806.         $o->warn($info->msg);
  807.         return unless $info->ok;
  808.         }
  809.         my $type = $o->rep_ison($rep) ? "Active" : "Inactive";
  810.         my $num  = (
  811.         $o->rep_ison($rep)
  812.         ? " " . find_index($rep, 1, @reps_on)
  813.         : ""
  814.         );
  815.         my @info = $info->result_l;
  816.         my @keys = qw(Name Location Type);
  817.         push @keys, qw(Username) if @info >= 4;
  818.         push @keys, qw(Password) if @info >= 5;
  819.         $o->inform("Describing $type Repository$num:\n");
  820.         $o->print_pairs(\@keys, \@info);
  821.         return 1;
  822.     }
  823.     elsif (matches($cmd, 'r|ename')) {
  824.         my $name = pop @args;
  825.         my $arg = join(' ', @args);
  826.         my $rep = $arg;
  827.         if ($arg =~ /^\d+$/) {
  828.         return unless $o->assert(
  829.             bounded(1, $arg, scalar @reps_on),
  830.             "no such active repository $arg"
  831.         );
  832.         $rep = $reps_on[$arg - 1];
  833.         }
  834.         else {
  835.         $rep = $o->rep_uniq($rep);
  836.         return unless $o->assert(
  837.             $o->rep_exists($rep),
  838.             "no such repository '$rep'"
  839.         );
  840.         }
  841.         my $ok = PPM::UI::repository_rename($rep, $name);
  842.         unless ($ok->is_success) {
  843.         $o->warn($ok->msg);
  844.         return unless $ok->ok;
  845.         }
  846.         $o->rep_on($name) if $o->rep_ison($rep);
  847.         $o->rep_off($rep);
  848.         $o->cache_clear('search');
  849.     }
  850.  
  851.     #=====================================================================
  852.     # On, off, up, and down commands:
  853.     #=====================================================================
  854.     elsif (matches($cmd, 'on')) {
  855.         my $rep = $o->rep_uniq(join(' ', @args));
  856.         return unless $o->assert(
  857.         $o->rep_isoff($rep),
  858.         "no such inactive repository '$rep'"
  859.         );
  860.         $o->rep_on($rep);
  861.         $o->cache_clear('search');
  862.     }
  863.     elsif (matches($cmd, 'of|f')) {
  864.         my $arg = join(' ', @args);
  865.         my $rep = $arg;
  866.         if ($arg =~ /^\d+$/) {
  867.         return unless $o->assert(
  868.             bounded(1, $arg, scalar @reps_on),
  869.             "no such active repository $arg"
  870.         );
  871.         $rep = $reps_on[$arg - 1];
  872.         }
  873.         else {
  874.         $rep = $o->rep_uniq($rep);
  875.         return unless $o->assert(
  876.             $o->rep_exists($rep),
  877.             "no such repository '$rep'"
  878.         );
  879.         }
  880.         $o->rep_off($rep);
  881.         $o->cache_clear('search');
  882.     }
  883.     elsif (matches($cmd, 'up')) {
  884.         my $arg = join(' ', @args);
  885.         my $rep = $arg;
  886.         if ($arg =~ /^\d+$/) {
  887.         return unless $o->assert(
  888.             bounded(1, $arg, scalar @reps_on),
  889.             "no such active repository $arg"
  890.         );
  891.         $rep = $reps_on[$arg - 1];
  892.         }
  893.         else {
  894.         $rep = $o->rep_uniq($rep);
  895.         return unless $o->assert(
  896.             $o->rep_exists($rep),
  897.             "no such repository '$rep'"
  898.         );
  899.         }
  900.         $o->rep_up($rep);
  901.     }
  902.     elsif (matches($cmd, 'do|wn')) {
  903.         my $arg = join(' ', @args);
  904.         my $rep = $arg;
  905.         if ($arg =~ /^\d+$/) {
  906.         return unless $o->assert(
  907.             bounded(1, $arg, scalar @reps_on),
  908.             "no such active repository $arg"
  909.         );
  910.         $rep = $reps_on[$arg - 1];
  911.         }
  912.         else {
  913.         $rep = $o->rep_uniq($rep);
  914.         return unless $o->assert(
  915.             $o->rep_exists($rep),
  916.             "no such repository '$rep'"
  917.         );
  918.         }
  919.         $o->rep_down($rep);
  920.     }
  921.  
  922.     else {
  923.         $o->warn(<<END) and return;
  924. No such repository command '$cmd'; see 'help repository'.
  925. END
  926.     }
  927.     }
  928.     &$refresh;
  929.     unless(@reps) {
  930.     $o->warn("No repositories. Use 'rep add' to add a repository.\n");
  931.     }
  932.     else {
  933.     my $i = 0;
  934.     my $count = @reps_on;
  935.     my $l = length($count);
  936.     $o->inform("Repositories:\n");
  937.     for my $r (@reps_on) {
  938.         my $n = sprintf("%${l}d", $i + 1);
  939.         $o->inform("[$n] $r\n");
  940.         $i++;
  941.     }
  942.     for my $r ($o->dictsort(@reps_off)) {
  943.         my $s = ' ' x $l;
  944.         $o->inform("[$s] $r\n");
  945.     }
  946.     }
  947.     1;
  948. }
  949.  
  950. #============================================================================
  951. # Search:
  952. # search        # displays previous searches
  953. # search <\d+>        # displays results of previous search
  954. # search <terms>    # executes a new search on the current repository
  955. #============================================================================
  956. sub smry_search { "searches for packages in a repository" }
  957. sub help_search { <<'END' }
  958. search -- Search for Packages
  959.   Synopsis
  960.      search                Displays list of previous searches
  961.      search <number>       Displays results of search <number>
  962.      search <glob pattern> Performs a new search
  963.      search <field>=<glob> Searches for all packages matching the field.
  964.      search *              Displays all packages in the current repository
  965.  
  966.     The available fields are 'ABSTRACT', 'NAME', 'TITLE', 'AUTHOR', and
  967.     'VERSION'. 'NAME' is used when you do not specify a field.
  968.  
  969.   Description
  970.     Use the search command to look through the repository for packages. PPM
  971.     version 3 provides powerful search functionality. For example:
  972.  
  973.     1.  Search for 'CGI' anywhere in the name:
  974.  
  975.           search CGI
  976.  
  977.         Example results:
  978.  
  979.           Apache-CGI
  980.           CGI-Application
  981.           CGI-ArgChecker
  982.  
  983.     2.  Search for 'CGI' at the beginning of the name:
  984.  
  985.           search CGI*
  986.  
  987.         Example results:
  988.  
  989.           CGI-ArgChecker
  990.           CGI-Application
  991.  
  992.     3.  Search for all modules authored by someone with 'smith' in their
  993.         name or email:
  994.  
  995.           search AUTHOR=smith 
  996.  
  997.         Example results:
  998.  
  999.           Apache-ProxyPass
  1000.           Business-ISBN
  1001.  
  1002.     4.  Search for 'compress' anywhere in the abstract:
  1003.  
  1004.           search ABSTRACT=compress
  1005.  
  1006.         Example results:
  1007.  
  1008.           Apache-GzipChain
  1009.           IO-Zlib
  1010.  
  1011.     5.  Search for 'CGI' in the name, or 'web' in the abstract:
  1012.  
  1013.           search CGI or ABSTRACT=web
  1014.  
  1015.         Example results:
  1016.  
  1017.           CGI-XMLForm
  1018.           HTML-Clean
  1019.  
  1020.     6.  Search for 'XML' in the name and either 'parser' in the name or
  1021.         'pars' in the abstract, but not with 'XPath' in the name:
  1022.  
  1023.           search XML and (parser or ABSTRACT=pars) and not XPath
  1024.  
  1025.         Example results:
  1026.  
  1027.           XML-Node
  1028.           XML-Parser-EasyTree
  1029.  
  1030.     7.  PPM Server 3 repositories only: search by module name, even if
  1031.         unrelated to the containing package:
  1032.  
  1033.           search Data::Grove
  1034.                                 
  1035.         Example results:
  1036.  
  1037.           libxml-perl
  1038.  
  1039.     8.  Browse all packages in the repository:
  1040.  
  1041.           search *
  1042.  
  1043.         Example results:
  1044.  
  1045.           Affix-Infix2Postfix
  1046.           AI-Fuzzy
  1047.           [many more...]
  1048.  
  1049.     Recall previous searches using the 'search <number>' command. PPM stores
  1050.     searches for each session until you exit PPM.
  1051.  
  1052.     Some package names or versions are too long to be displayed in the
  1053.     search results. If a name is too long, you will see a '~' (tilde) as the
  1054.     last visible character in the column. You can use *describe* to view
  1055.     detailed information about such packages.
  1056.  
  1057.   Search Results
  1058.     When you type a command like "search XML", PPM searches in each of the
  1059.     Active Repositories (see the *repository* command) for your package. The
  1060.     results are merged into one list, and duplicates (packages found in more
  1061.     than one repository) are hidden.
  1062.  
  1063.     You can control what fields PPM shows for each package. The fields each
  1064.     have a built-in weight, which is used to calculate how wide to make each
  1065.     field based on the width of your screen. Information that doesn't fit
  1066.     into a field is truncated, and a tilde ("~") character is displayed in
  1067.     the last column of the field.
  1068.  
  1069.     Let's get down to an example:
  1070.  
  1071.         ppm> search XML
  1072.         Searching in Active Repositories
  1073.             1. CGI-XMLForm           [0.10] Extension to CGI.pm which
  1074.             2. Data-DumpXML          [1.01] Dump arbitrary data structures
  1075.             3. DBIx-XML_RDB          [0.05] Perl extension for creating XML
  1076.             4. DBIx-XMLMessage       [0.03] XML Message exchange between DBI
  1077.             5. GoXML-XQI            [1.1.4] Perl extension for the XML Query
  1078.             6. Language-DATR-DATR2~ [0.901] manipulate DATR .dtr, XML, HTML,
  1079.             7. libxml-perl           [0.07] support for deeply nested
  1080.             8. Mail-FilterXML         [0.1] Undetermined
  1081.             9. Mail-XML              [0.03] Adds a toXML() method to
  1082.            10. Pod-XML               [0.93] Module to convert POD to XML
  1083.  
  1084.     As you can see, the three fields being displayed are:
  1085.  
  1086.     1   NAME
  1087.  
  1088.         The package name
  1089.  
  1090.     2   VERSION
  1091.  
  1092.         The package version
  1093.  
  1094.     3   ABSTRACT
  1095.  
  1096.         The package abstract
  1097.  
  1098.     You can customize the view somewhat. If you want to view the authors,
  1099.     but not the abstract, you can run the same *search* command after using
  1100.     *set* to change the fields:
  1101.  
  1102.         ppm> set fields="NAME VERSION AUTHOR"
  1103.         Setting 'fields' set to 'name version author'.
  1104.         ppm> search XML
  1105.         Using cached search result set 1.
  1106.             1. CGI-XMLForm         [0.10] Matt Sergeant (matt@sergeant.org)
  1107.             2. Data-DumpXML        [1.01] Gisle Aas (gisle@aas.no)
  1108.             3. DBIx-XML_RDB        [0.05] Matt Sergeant (matt@sergeant.org)
  1109.             4. DBIx-XMLMessage     [0.03] Andrei Nossov (andrein@andrein.com)
  1110.             5. GoXML-XQI          [1.1.4] Matthew MacKenzie (matt@goxml.com)
  1111.             6. Language-DATR-DAT~ [0.901] Lee Goddard (lgoddard@cpan.org)
  1112.             7. libxml-perl         [0.07] Ken MacLeod (ken@bitsko.slc.ut.us)
  1113.             8. Mail-FilterXML       [0.1] Matthew MacKenzie (matt@goxml.com)
  1114.             9. Mail-XML            [0.03] Matthew MacKenzie (matt@goxml.com)
  1115.            10. Pod-XML             [0.93] Matt Sergeant (matt@sergeant.org)
  1116.  
  1117.     You can change the order in which the results are sorted, and what
  1118.     columns are displayed. The settings *fields* and *sort-field* changes
  1119.     this. You can sort by any valid field name (even fields which are not
  1120.     displayed). See the *settings* command for the valid field names.
  1121.  
  1122.     PPM always hides "duplicate" results. It decides whether a result is
  1123.     duplicated based on the fields being displayed. If the same package is
  1124.     found in more than one repository, but you don't have the REPOSITORY
  1125.     field showing, PPM will only list the package once.
  1126. END
  1127. sub comp_search {()}
  1128. sub run_search {
  1129.     my $o = shift;
  1130.     my @args = @_;
  1131.     my $query = $o->raw_args || join ' ', @args;
  1132.     trace(1, "PPM: search @args\n\tquery='$query'\n");
  1133.     return unless $o->assert(
  1134.     scalar $o->reps_on,
  1135.     "you must activate a repository before searching."
  1136.     );
  1137.  
  1138.     # No args: show cached result sets
  1139.     unless (@args) {
  1140.     my @search_results = $o->cache_sets('search');
  1141.     my $search_result_current = $o->cache_set_current('search');
  1142.     if (@search_results) {
  1143.         $o->inform("Search Result Sets:\n");
  1144.         my $i = 0;
  1145.         for (@search_results) {
  1146.         $o->informf("%s%2d",
  1147.                $search_result_current == $i ? "*" : " ",
  1148.                $i + 1);
  1149.         $o->inform(". $_->{query}\n");
  1150.         $i++;
  1151.         }
  1152.     }
  1153.     else {
  1154.         $o->warn("No search result sets -- provide a search term.\n");
  1155.         return;
  1156.     }
  1157.     }
  1158.  
  1159.     # Args:
  1160.     else {
  1161.     # Show specified result set
  1162.     if ($query =~ /^\d+/) {
  1163.         my $set = int($query);
  1164.         my $s = $o->cache_set('search', $set - 1);
  1165.         unless ($set > 0 and defined $s) {
  1166.         $o->warn("No such search result set '$set'.\n");
  1167.         return;
  1168.         }
  1169.  
  1170.         $query = $o->cache_set('search', $set-1, 'query');
  1171.         $o->inform("Search Results Set $set ($query):\n");
  1172.         $o->print_formatted($s, $o->cache_set_index('search'));
  1173.         $o->cache_set_current('search', $set-1);
  1174.         $o->cache_set_index('search', -1);
  1175.     }
  1176.        
  1177.     # Query is the same as a previous query on the same repository: 
  1178.     # Use cached results and set them as default
  1179.     elsif(grep { $_->{query} eq $query } $o->cache_sets('search')) {
  1180.         my @entries = $o->cache_sets('search');
  1181.         for (my $i=0; $i<@entries; $i++) {
  1182.         if ($o->cache_set('search', $i, 'query') eq $query) {
  1183.             $o->inform("Using cached search result set ", $i+1, ".\n");
  1184.             $o->cache_set_current('search', $i);
  1185.             my $set = $o->cache_set('search');
  1186.             $o->print_formatted($set);
  1187.         }
  1188.         }
  1189.     }
  1190.  
  1191.     # Perform a new search
  1192.     else {
  1193.         my @rlist = $o->reps_on;
  1194.         my $targ = $o->conf('target');
  1195.         my $case = not $o->conf('case-sensitivity');
  1196.  
  1197.         $o->inform("Searching in Active Repositories\n");
  1198.         my $ok = PPM::UI::search(\@rlist, $targ, $query, $case);
  1199.         unless ($ok->is_success) {
  1200.         $o->warn($ok->msg);
  1201.         return unless $ok->ok;
  1202.         }
  1203.         my @matches = $ok->result_l;
  1204.         unless (@matches) {
  1205.         $o->warn("No matches for '$query'; see 'help search'.\n");
  1206.         return 1;
  1207.         }
  1208.         $o->cache_set_index('search', -1);
  1209.         $o->cache_set_add('search', $query, \@matches);
  1210.         $o->cache_set_current('search', scalar($o->cache_sets('search')) - 1);
  1211.         my @set = $o->cache_set('search');
  1212.         $o->print_formatted(\@set);
  1213.     }
  1214.     }
  1215.     1;
  1216. }
  1217. sub alias_search { qw(s) }
  1218.  
  1219. #============================================================================
  1220. # tree
  1221. # tree        # shows the dependency tree for the default/current pkg
  1222. # tree <\d+>    # shows dep tree for numbered pkg in current search set
  1223. # tree <pkg>    # shows dep tree for given package
  1224. # tree <url>    # shows dep tree for package located at <url>
  1225. # tree <glob>    # searches for matches
  1226. #============================================================================
  1227. sub smry_tree { "shows package dependency tree" }
  1228. sub help_tree { <<'END' }
  1229. tree -- Show Dependency Tree for Packages
  1230.   Synopsis
  1231.      tree                Displays the dependency-tree of the current
  1232.                          or default package
  1233.      tree <number>       Displays the dependency-tree of the given <number>
  1234.      tree <range>        Displays a <range> of dependency-trees
  1235.      tree <package name> Displays the dependency-tree of the named package
  1236.      tree <url>          Displays the dependency-tree for the
  1237.                          package at <url>
  1238.      tree <glob pattern> Performs a new search using <glob pattern>
  1239.  
  1240.   Description
  1241.     The tree command is used to show the "dependency tree" of a given
  1242.     package (additional packages that are required by the current package).
  1243.     For example:
  1244.  
  1245.         tree SOAP-lite
  1246.  
  1247.     returns:
  1248.  
  1249.         ====================
  1250.         SOAP-Lite 0.51
  1251.         |__MIME-tools 5.316
  1252.         |   |__MailTools 1.15
  1253.         |   \__IO-stringy 1.216
  1254.         |
  1255.         \__MIME-Lite 2.105
  1256.         ====================
  1257.  
  1258.     SOAP-Lite requires four other packages.
  1259.  
  1260.     When tree is called without a <name> or <number> switch, the command
  1261.     will return the dependency tree of the first package in the default
  1262.     search result. If there is no default search, you will be requested to
  1263.     use search to find a package.
  1264. END
  1265. sub comp_tree { goto &comp_describe }
  1266. sub run_tree {
  1267.     my $o = shift;
  1268.     my @args = @_;
  1269.     trace(1, "PPM: tree @args\n");
  1270.  
  1271.     # Check for anything that looks like a query. If it does, just
  1272.     # send it to search() instead.
  1273.     my $query = $o->raw_args || join ' ', @args;
  1274.     $query ||= '';
  1275.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  1276.     $o->inform("Wildcards detected; using 'search' instead...\n");
  1277.     return $o->run('search', @_);
  1278.     }
  1279.  
  1280.     # No Args: describes current index of current result set, or 1.
  1281.     unless (@args) {
  1282.     my @search_results = $o->cache_sets('search');
  1283.     my $search_result_current = $o->cache_set_current('search');
  1284.     unless (@search_results and
  1285.         bounded(0, $search_result_current, $#search_results)) {
  1286.         $o->warn("No search results to show dependency tree for -- " . 
  1287.           "use 'search' to find a package.\n");
  1288.         return;
  1289.     }
  1290.     else {
  1291.         my @res = $o->cache_set('search');
  1292.         my $npkgs = @res;
  1293.         $o->inform("$SEP\n");
  1294.         if ($o->cache_entry('search')) {
  1295.         my $n = $o->cache_set_index('search') + 1;
  1296.         $o->inform("Package $n:\n");
  1297.         $o->tree_pkg($o->cache_entry('search'));
  1298.         }
  1299.         elsif (defined $o->cache_entry('search', 0)) {
  1300.         $o->inform("Package 1:\n");
  1301.         $o->tree_pkg($o->cache_entry('search', 0));
  1302.         $o->cache_set_index('search', 0);
  1303.         }
  1304.         else {
  1305.         $o->inform("Search Results are empty -- use 'search' again.\n");
  1306.         }
  1307.         $o->inform("$SEP\n");
  1308.     }
  1309.     }
  1310.  
  1311.     # Args provided
  1312.     else {
  1313.  
  1314.     # Describe a particular number:
  1315.     if (my @r = parse_range(@args)) {
  1316.         my @search_results = $o->cache_sets('search');
  1317.         my $search_result_current = $o->cache_set_current('search');
  1318.         unless (bounded(0, $search_result_current, $#search_results)) {
  1319.         $o->inform("No search results to show dependency tree for -- " . 
  1320.           "use 'search' to find a package.\n");
  1321.         return;
  1322.         }
  1323.         else {
  1324.         for my $n (@r) {
  1325.             my $sr = $o->cache_set('search');
  1326.             $o->inform("$SEP\n");
  1327.             if (bounded(1, $n, scalar @$sr)) {
  1328.             $o->inform("Package $n:\n");
  1329.             $o->tree_pkg($o->cache_entry('search', $n-1));
  1330.             }
  1331.             else {
  1332.             $o->inform("No such package $n in result set.\n");
  1333.             }
  1334.             $o->cache_set_index('search', $n - 1);
  1335.         }
  1336.         $o->inform("$SEP\n");
  1337.         }
  1338.     }
  1339.  
  1340.     # Describe a particular package
  1341.     else {
  1342.         return unless $o->assert(
  1343.         scalar $o->reps_on,
  1344.         "No repositories -- use 'rep add' to add a repository.\n"
  1345.         );
  1346.         my $pkg =
  1347.           PPM::UI::describe([$o->reps_on], $o->conf('target'), $args[0]);
  1348.         unless ($pkg->is_success) {
  1349.         $o->warn($pkg->msg);
  1350.         return unless $pkg->ok;
  1351.         }
  1352.         if ($pkg->ok) {
  1353.         $o->inform("$SEP\n");
  1354.         $o->tree_pkg($pkg->result);
  1355.         $o->inform("$SEP\n");
  1356.         }
  1357.     }
  1358.     }
  1359.     1;
  1360. }
  1361.  
  1362. #============================================================================
  1363. # Describe:
  1364. # des        # describes default or current package
  1365. # des <\d+>    # describes numbered package in the current search set
  1366. # des <pkg>    # describes the named package (bypasses cached results)
  1367. # des <url>    # describes the package located at <url>
  1368. #============================================================================
  1369. sub smry_describe { "describes packages in detail" }
  1370. sub help_describe { <<'END' }
  1371. describe -- Describe Packages
  1372.   Synopsis
  1373.      des                Describes default/current package
  1374.      des <number>       Describes package <number> in the
  1375.                         current search set
  1376.      des <range>        Describes packages in the given 
  1377.                         <range> from the current search
  1378.      des <package name> Describes named package
  1379.      des <url>          Describes package located at <url>
  1380.      des <glob pattern> Performes a new search using <glob pattern>
  1381.  
  1382.   Description
  1383.     The describe command returns information about a package, including the
  1384.     name of the package, the author's name and a brief description (called
  1385.     an "Abstract") about the package. For example:
  1386.  
  1387.         describe libnet
  1388.  
  1389.     returns:
  1390.  
  1391.         ===============================
  1392.         Package 1
  1393.         Name: libnet
  1394.         Version: 1.07.03
  1395.         Author: Graham Barr
  1396.         Abstract: Collection of Network protocol modules
  1397.         Implementations:
  1398.                 1.sun4-solaris-thread-multi
  1399.                 2.i686-linux-thread-multi
  1400.                 3.MSWIn32-x86-multi-thread
  1401.         ===============================
  1402.  
  1403.     There are two modifiers to the describe command:
  1404.  
  1405.     -ppd
  1406.         Displays the raw PPD of the package.
  1407.  
  1408.     -dump
  1409.         The same as -ppd.
  1410.  
  1411.     When the describe command is called without arguments, it returns
  1412.     information about the first package in the current search. If there is
  1413.     no default search set, you will be prompted to use the search command to
  1414.     find a package.
  1415.  
  1416.     If describe is called with a numeric argument, that number is set as the
  1417.     default package and the information about that package is returned. If
  1418.     the number given doesn't exist, you will be prompted to use search to
  1419.     find a package. Also, you can use describe to get descriptions of
  1420.     several packages. For example:
  1421.  
  1422.         describe 4-7
  1423.  
  1424.     will return descriptions of packages 4 through 7 in the current search
  1425.     request. You can also enter:
  1426.  
  1427.         describe 3-4,10
  1428.  
  1429.     to get information on packages 3, 4 and 10.
  1430.  
  1431.     If you specify a URL as the argument to describe, PPM will describe the
  1432.     package located at the URL. The URL must point to a PPD file. The URL
  1433.     can also point to a PPD file on your computer.
  1434.  
  1435.     When the describe command is given a name with a wildcard (such as "*"
  1436.     or "?") it executes the search command with the given argument. For
  1437.     example, describe Tk* will return the name(s) of any packages that match
  1438.     the search parameters.
  1439.  
  1440.   See Also
  1441.     properties
  1442. END
  1443. sub comp_describe {
  1444.     my $o = shift;
  1445.     my ($word, $line, $start) = @_;
  1446.  
  1447.     # If no search results
  1448.     my $n_results = $o->cache_sets('search');
  1449.     my $n_current = $o->cache_set_current('search');
  1450.     return ()
  1451.       unless ($n_results and bounded(0, $n_current, $n_results - 1));
  1452.     my @words = $o->line_parsed($line);
  1453.  
  1454.     # If the previous word isn't a number or the command, stop.
  1455.     return ()
  1456.       if ($#words > 0 and
  1457.       $words[$#words] !~ /^\d+/ and
  1458.       $start == length($line) or 
  1459.       $#words > 1);
  1460.  
  1461.     # This is the most optimistic list:
  1462.     my @results = $o->cache_set('search');
  1463.     my $npkgs = @results;
  1464.     my @compls = (1 .. $npkgs);
  1465.  
  1466.     # If the previous word is a number, return only other numbers:
  1467.     return $o->completions($word, \@compls)
  1468.       if $words[$#words] =~ /^\d+/;
  1469.  
  1470.     # Either a number or the names of the packages
  1471.     push @compls, map { $_->name } @results;
  1472.     return $o->completions($word, \@compls);
  1473. }
  1474. sub run_describe {
  1475.     my $o = shift;
  1476.     my @args = @_;
  1477.     
  1478.     # Check for options:
  1479.     my $ppd;
  1480.     {
  1481.     local @ARGV = @args;
  1482.     GetOptions(ppd => \$ppd, dump => \$ppd);
  1483.     @args = @ARGV;
  1484.     }
  1485.  
  1486.     trace(1, "PPM: describe @args\n");
  1487.  
  1488.     # Check for anything that looks like a query. If it does, just
  1489.     # send it to search() instead.
  1490.     my $query = $o->raw_args || join ' ', @args;
  1491.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  1492.     $o->inform("Wildcards detected; using 'search' instead...\n");
  1493.     return $o->run('search', @_);
  1494.     }
  1495.  
  1496.     my $dumper = sub {
  1497.     my $o = shift;
  1498.     my $pkg_obj = shift;
  1499.     my $ppd = $pkg_obj->getppd($o->conf('target'))->result;
  1500.     $o->page($ppd);
  1501.     };
  1502.     my $displayer = $ppd ? $dumper : \&describe_pkg;
  1503.  
  1504.     # No Args: describes current index of current result set, or 1.
  1505.     unless (@args) {
  1506.     my @search_results = $o->cache_sets('search');
  1507.     my $search_result_current = $o->cache_set_current('search');
  1508.     unless (@search_results and
  1509.         bounded(0, $search_result_current, $#search_results)) {
  1510.         $o->warn("No search results to describe -- " . 
  1511.           "use 'search' to find a package.\n");
  1512.         return;
  1513.     }
  1514.     else {
  1515.         my @res = $o->cache_set('search');
  1516.         my $npkgs = @res;
  1517.         $o->inform("$SEP\n");
  1518.         if ($o->cache_entry('search')) {
  1519.         my $n = $o->cache_set_index('search') + 1;
  1520.         $o->inform("Package $n:\n");
  1521.         $o->$displayer($o->cache_entry('search'));
  1522.         }
  1523.         elsif (defined $o->cache_entry('search', 0)) {
  1524.         $o->inform("Package 1:\n");
  1525.         $o->$displayer($o->cache_entry('search', 0));
  1526.         $o->cache_set_index('search', 0);
  1527.         }
  1528.         else {
  1529.         $o->warn("Search Results are empty -- use 'search' again.\n");
  1530.         }
  1531.         $o->inform("$SEP\n");
  1532.     }
  1533.     }
  1534.  
  1535.     # Args provided
  1536.     else {
  1537.  
  1538.     # Describe a particular number:
  1539.     if (my @r = parse_range(@args)) {
  1540.         my @search_results = $o->cache_sets('search');
  1541.         my $search_result_current = $o->cache_set_current('search');
  1542.         unless (bounded(0, $search_result_current, $#search_results)) {
  1543.         $o->warn("No search results to describe -- " . 
  1544.           "use 'search' to find a package.\n");
  1545.         return;
  1546.         }
  1547.         else {
  1548.         for my $n (@r) {
  1549.             my $sr = $o->cache_set('search');
  1550.             $o->inform("$SEP\n");
  1551.             if (bounded(1, $n, scalar @$sr)) {
  1552.             $o->inform("Package $n:\n");
  1553.             $o->$displayer($o->cache_entry('search', $n-1));
  1554.             }
  1555.             else {
  1556.             $o->inform("No such package $n in result set.\n");
  1557.             }
  1558.             $o->cache_set_index('search', $n - 1);
  1559.         }
  1560.         $o->inform("$SEP\n");
  1561.         }
  1562.     }
  1563.  
  1564.     # Describe a particular package
  1565.     else {
  1566.         return unless $o->assert(
  1567.         scalar $o->reps_on,
  1568.         "No repositories -- use 'rep add' to add a repository.\n"
  1569.         );
  1570.         my ($set, $index) = $o->cache_find('search', $args[0]);
  1571.         my ($ok, $pkg);
  1572.         if ($index >= 0) {
  1573.         $o->cache_set_current('search', $set);
  1574.         $o->cache_set_index('search', $index);
  1575.         $pkg = $o->cache_entry('search');
  1576.         }
  1577.         else {
  1578.         $ok = PPM::UI::describe([$o->reps_on],
  1579.                     $o->conf('target'), $args[0]);
  1580.         unless ($ok->is_success) {
  1581.             $o->inform($ok->msg);
  1582.             return unless $ok->ok;
  1583.         }
  1584.         $pkg = $ok->result;
  1585.         $o->cache_set_add('search', $args[0], [$pkg]);
  1586.         my $last = $o->cache_sets('search') - 1;
  1587.         $o->cache_set_current('search', $last);
  1588.         $o->cache_set_index('search', 0);
  1589.         }
  1590.         $o->inform("$SEP\n");
  1591.         $o->$displayer($pkg);
  1592.         $o->inform("$SEP\n");
  1593.     }
  1594.     }
  1595.     1;
  1596. }
  1597.  
  1598. #============================================================================
  1599. # Install:
  1600. # i        # installs default or current package
  1601. # i <\d+>    # installs numbered package in current search set
  1602. # i <pkg>    # installs named package
  1603. # i <url>    # installs the package at <url>
  1604. #============================================================================
  1605. sub smry_install { "installs packages" }
  1606. sub help_install { <<'END' }
  1607. install -- Install Packages
  1608.   Synopsis
  1609.      install           Installs default package
  1610.      install <number>  Installs packages by a specific <number>
  1611.      install <range>   Installs packages in the given numeric <range>
  1612.      install <name>    Installs named package
  1613.      install <url>     Installs the package located at <url>
  1614.  
  1615.   Description
  1616.     The install command is used to install packages from the repository.
  1617.     Install packages by name or number (the number is given by the
  1618.     repository or search request), or set a default package using the
  1619.     describe command. You can specify a full URL to a PPD file; the URL may
  1620.     point to a PPD file on your computer.
  1621.  
  1622.     If you have profile tracking enabled, (see 'help profile') the current
  1623.     profile will be updated to include the newly installed package(s).
  1624.  
  1625.     The following modifiers can be used with the install command:
  1626.  
  1627.     *   -force
  1628.  
  1629.     *   -noforce
  1630.  
  1631.     *   -follow
  1632.  
  1633.     *   -nofollow
  1634.  
  1635.     The force and follow switches determine how packages are installed:
  1636.  
  1637.      FORCE       FOLLOW          RESULT
  1638.      false       false           Checks to see if the package is installed and
  1639.                                  if it is, installation stops. If there are any
  1640.                                  missing prerequisites, the installation will
  1641.                                  fail.
  1642.  
  1643.      false       true            Checks to see if the package is installed and
  1644.                                  if it is, installation stops. If there are any
  1645.                                  missing prerequisites, they are automatically
  1646.                                  installed. NOTE: this is the default setting
  1647.                                  when PPM is first installed.
  1648.  
  1649.      true        false           If the package is installed, PPM will
  1650.                                  reinstall the package. If there are any
  1651.                                  missing prerequisites, the installation will
  1652.                                  fail.
  1653.  
  1654.      true        true            If the package is installed, PPM will
  1655.                                  reinstall the package. All prerequisites are
  1656.                                  installed, missing or not.
  1657.     
  1658.     If you do not specify any options, install uses the default settings.
  1659.     Set or view the current defaults using the 'settings' command.
  1660.  
  1661.     For example:
  1662.  
  1663.         install foo
  1664.  
  1665.     will install the package named "foo", using the default settings.
  1666.     Over-ride the defaults using the install modifiers described above.
  1667.  
  1668.     For example:
  1669.  
  1670.         install foo -force
  1671.  
  1672.     will install the "foo" package, even if it has already been installed.
  1673.     If both -force and -follow are set to "true", all the prerequisites for
  1674.     any package you install will also be installed. For example, the
  1675.     installation of a tk-related package, like "tk-ach" which is 8.4 kB will
  1676.     be preceded by the installation of Tk, which is 1.7 MB.
  1677.  
  1678.     You can also install by package number. Package numbers are based on the
  1679.     current repository or current search request. For example:
  1680.  
  1681.         install 6
  1682.  
  1683.     installs package number 6. You can install more than one package at one
  1684.     time:
  1685.  
  1686.         install 3-5
  1687.  
  1688.     installs packages 3, 4 and 5. You can also type install 3-6,8 to install
  1689.     packages 3,4,5,6 and 8.
  1690.  
  1691.   See Also
  1692.     profile
  1693. END
  1694. sub comp_install { goto &comp_describe }
  1695. sub run_install {
  1696.     my $o = shift;
  1697.     my @args = @_;
  1698.     trace(1, "PPM: install @args\n");
  1699.  
  1700.     # Get the install options
  1701.     my %opts = (
  1702.     force  => $o->conf('force-install'),
  1703.     follow => $o->conf('follow-install'),
  1704.     dryrun => 0,
  1705.     );
  1706.     {
  1707.     local @ARGV = @args;
  1708.     GetOptions('force!'  => \$opts{force},
  1709.            'follow!' => \$opts{follow},
  1710.            'dryrun'  => \$opts{dryrun},
  1711.           );
  1712.     @args = @ARGV;
  1713.     }
  1714.  
  1715.     # No Args -- installs default package
  1716.     unless (@args) {
  1717.     my @search_results = $o->cache_sets('search');
  1718.     my $search_result_current = $o->cache_set_current('search');
  1719.     unless (@search_results and
  1720.         bounded(0, $search_result_current, $#search_results)) {
  1721.         $o->warn("No search results to install -- " . 
  1722.           "use 'search' to find a package.\n");
  1723.         return;
  1724.     }
  1725.     else {
  1726.         my @results = $o->cache_set('search');
  1727.         my $npkgs = @results;
  1728.         my $pkg;
  1729.         if ($o->cache_entry('search')) {
  1730.         my $n = $o->cache_set_index('search') + 1;
  1731.         $o->inform("Package $n:\n");
  1732.         $pkg = $o->cache_entry('search');
  1733.         }
  1734.         else {
  1735.         $o->inform("Package 1:\n");
  1736.         $pkg = $o->cache_entry('search', 0);
  1737.         }
  1738.         return $o->install_pkg($pkg, \%opts);
  1739.     }
  1740.     }
  1741.  
  1742.     # Args provided
  1743.     else {
  1744.  
  1745.     # Install a particular number:
  1746.     if (my @r = parse_range(@args)) {
  1747.         my @search_results = $o->cache_sets('search');
  1748.         my $search_result_current = $o->cache_set_current('search');
  1749.         unless (@search_results and
  1750.             bounded(0, $search_result_current, $#search_results)) {
  1751.         $o->warn("No search results to install -- " . 
  1752.           "use 'search' to find a package.\n");
  1753.         return;
  1754.         }
  1755.         else {
  1756.         my $ok = 0;
  1757.         for my $n (@r) {
  1758.             my $sr = $o->cache_set('search');
  1759.             if (bounded(1, $n, scalar @$sr)) {
  1760.             $o->inform("Package $n:\n");
  1761.             my $pkg = $sr->[$n-1];
  1762.             $ok++ if $o->install_pkg($pkg, \%opts);
  1763.             }
  1764.             else {
  1765.             $o->inform("No such package $n in result set.\n");
  1766.             }
  1767.         }
  1768.         return unless $ok;
  1769.         }
  1770.     }
  1771.  
  1772.     # Install a particular package
  1773.     else {
  1774.         unless ($o->reps_all) {
  1775.         $o->warn("Can't install: no repositories defined.\n");
  1776.         }
  1777.         else {
  1778.         return $o->install_pkg($args[0], \%opts);
  1779.         }
  1780.         return;
  1781.     }
  1782.     }
  1783.     1;
  1784. }
  1785.  
  1786. #============================================================================
  1787. # Target:
  1788. # t        # displays a list of backend targets
  1789. # t [set] <\d+>    # sets numbered target as default backend target
  1790. # t des [<\d+>]    # describes the given (or default) target
  1791. #============================================================================
  1792. sub smry_targets { "views or sets target installer backends" }
  1793. sub help_targets { <<'END' }
  1794. targets -- View Target Installer Backends
  1795.   Synopsis
  1796.      target                      Displays a list of backend targets
  1797.      target <number>             Sets <number> as default backend target
  1798.      target [select] <name or num>
  1799.                                  Sets <name or num> as default backend target
  1800.      target describe [name or num]
  1801.                                  Describes the given (or default) target
  1802.      target set <key> <val>      Sets the target's <key> to <val> 
  1803.      target rename <name or num> <name>
  1804.                                  Renames the given target to <name>
  1805.  
  1806.   Description
  1807.     The target is the destination location of the install routine, such as
  1808.     the directory where the packages are installed when they're downloaded
  1809.     from the repository. For example:
  1810.  
  1811.         target
  1812.  
  1813.     returns:
  1814.  
  1815.         Targets:
  1816.           1. ActivePerl 618
  1817.         * 2. ActivePerl 629
  1818.  
  1819.     This shows that there are two available targets, and that the second
  1820.     target (ActivePerl 629) is currently the default (as shown by the
  1821.     asterisk). Using multiple targets, you can manage multiple installations
  1822.     of Perl from a single command-line.
  1823. END
  1824. sub comp_targets {
  1825.     my $o = shift;
  1826.     my ($word, $line, $start) = @_;
  1827.     my @words = $o->line_parsed($line);
  1828.     my $words = scalar @words;
  1829.     my @compls;
  1830.     my @targs = PPM::UI::target_list()->result_l;
  1831.  
  1832.     # only return 'set' and 'describe' when we're completing the second word
  1833.     if ($words == 1 or $words == 2 and $start != length($line)) {
  1834.     @compls = ('set', 'select', 'describe', 'rename', 1 .. scalar @targs);
  1835.     return $o->completions($word, \@compls);
  1836.     }
  1837.  
  1838.     if ($words == 2 or $words == 3 and $start != length($line)) {
  1839.     # complete 'set'
  1840.     if (matches($words[1], 's|et')) {
  1841.         my $targ = $o->conf('target');
  1842.         @compls = map { $_->[0] }
  1843.               grep { $_->[1] }
  1844.               PPM::UI::target_config_keys($targ)->result_l;
  1845.         return $o->completions($word, \@compls);
  1846.     }
  1847.     # complete 'describe' and 'rename'
  1848.     elsif (matches($words[1], 'd|escribe')
  1849.         or matches($words[1], 'r|ename')
  1850.         or matches($words[1], 's|elect')) {
  1851.         return $o->completions($word, [1 .. scalar @targs]);
  1852.     }
  1853.     }
  1854.     ();
  1855. }
  1856. sub run_targets {
  1857.     my $o = shift;
  1858.     my @args = @_;
  1859.     trace(1, "PPM: target @args\n");
  1860.  
  1861.     my @targets = PPM::UI::target_list()->result_l;
  1862.     my $targets = @targets;
  1863.  
  1864.     # No arguments: print targets
  1865.     if (@args) {
  1866.     my ($cmd, @rest) = @args;
  1867.     if ($cmd =~ /^\d+$/
  1868.         or matches($cmd, 'se|lect')) {
  1869.         my $num =     $cmd =~ /^\d+$/        ? $cmd        :
  1870.             $rest[0] =~ /^\d+$/    ? $rest[0]    :
  1871.             do {
  1872.                 my $n = find_index($rest[0], 1, @targets);
  1873.                 if ($n < 1) {
  1874.                 $o->warn("No such target '$rest[0]'.\n");
  1875.                 return;
  1876.                 }
  1877.                 $n;
  1878.             };
  1879.  
  1880.         # QA the number: is it too high/low?
  1881.         unless(bounded(1, $num, $targets)) {
  1882.         $o->warn("No such target number '$num'.\n");
  1883.         return;
  1884.         }
  1885.         else {
  1886.         $o->conf('target', $targets[$num-1]);
  1887.         $o->cache_clear('query');
  1888.         }
  1889.     }
  1890.     elsif (matches($cmd, 'r|ename')) {
  1891.         my ($oldnum, $newname) = @rest;
  1892.         $oldnum =    $oldnum =~ /^\d+$/ ? $oldnum :
  1893.             do {
  1894.                 my $n = find_index($oldnum, 1, @targets);
  1895.                 if ($n < 1) {
  1896.                 $o->warn("No such target '$oldnum'.\n");
  1897.                 return;
  1898.                 };
  1899.                 $n;
  1900.             };
  1901.         unless (defined $oldnum && $oldnum =~ /^\d+$/) {
  1902.         $o->warn(<<END);
  1903. target: '$cmd' requires a numeric argument. See 'help $cmd'.
  1904. END
  1905.         return;
  1906.         }
  1907.         unless (bounded(1, $oldnum, $targets)) {
  1908.         $o->warn("No such target number '$oldnum'.\n");
  1909.         return;
  1910.         }
  1911.         unless (defined $newname and $newname) {
  1912.         $newname = '' unless defined $newname;
  1913.         $o->warn(<<END);
  1914. Target names must be non-empty: '$newname' is not a valid name.
  1915. END
  1916.         return;
  1917.         }
  1918.         
  1919.         my $oldname = $targets[$oldnum - 1];
  1920.         my $ret = PPM::UI::target_rename($oldname, $newname);
  1921.         $o->warn($ret->msg) unless $ret->ok;
  1922.         $o->conf('target', $newname)
  1923.           if $o->conf('target') eq $oldname;
  1924.         @targets = PPM::UI::target_list()->result_l;
  1925.         $targets = scalar @targets;
  1926.     }
  1927.     elsif (matches($cmd, "s|et")) {
  1928.         my ($key, $value) = @rest;
  1929.         if (defined $key and $key =~ /=/ and not defined $value) {
  1930.         ($key, $value) = split /=/, $key;
  1931.         }
  1932.         unless(defined($key) && $key) {
  1933.         $o->warn(<<END);
  1934. You must specify what option to set. See 'help target'.
  1935. END
  1936.         return;
  1937.         }
  1938.         unless(defined($value)) {
  1939.         $o->warn(<<END);
  1940. You must provide a value for the option. See 'help target'.
  1941. END
  1942.         return;
  1943.         }
  1944.         my $targ = $o->conf('target');
  1945.         my %keys = map { @$_ }
  1946.                PPM::UI::target_config_keys($targ)->result_l;
  1947.         unless ($keys{$key}) {
  1948.         $o->warn("Invalid set key '$key'; these are the settable values:\n");
  1949.         $o->warn("    $_\n") for (grep { $keys{$_} } keys %keys);
  1950.         return;
  1951.         }
  1952.         my $ok = PPM::UI::target_config_set($targ, $key, $value);
  1953.         unless ($ok->is_success) {
  1954.         $o->warn($ok->msg);
  1955.         return unless $ok->ok;
  1956.         }
  1957.         $o->inform("Target attribute '$key' set to '$value'\n");
  1958.         return 1;
  1959.     }
  1960.     elsif (matches($cmd, "d|escribe")) {
  1961.         my %opts = (exec => 1);
  1962.         my $sel;
  1963.         if (@rest) {
  1964.         local @ARGV = @rest;
  1965.         GetOptions(\%opts, 'exec!');
  1966.         @rest = @ARGV;
  1967.         }
  1968.         if (@rest) {
  1969.         $sel =    $rest[0] =~ /^\d+$/ ? $rest[0] :
  1970.                 do {
  1971.                 my $n = find_index($rest[0], 1, @targets);
  1972.                 if ($n < 1) {
  1973.                     $o->warn("No such target '$rest[0]'.\n");
  1974.                     return;
  1975.                 };
  1976.                 $n;
  1977.                 };
  1978.         unless(bounded(1, $sel, $targets)) {
  1979.             $o->warn("No such target number '$sel'.\n");
  1980.         }
  1981.         }
  1982.         else {
  1983.         $sel = find_index($o->conf('target'), 1, @targets);
  1984.         }
  1985.         my $targ = $targets[$sel-1];
  1986.         my (@keys, @vals);
  1987.         my $res = $opts{exec}
  1988.         ? PPM::UI::target_info($targ)
  1989.         : PPM::UI::target_raw_info($targ);
  1990.         unless ($res->is_success) {
  1991.         $o->warn($res->msg);
  1992.         return unless $res->ok;
  1993.         }
  1994.         my %h = $res->result_h;
  1995.         my @h = sort keys %h;
  1996.         push @keys, @h;
  1997.         push @vals, $h{$_} for @h;
  1998.         if ($opts{exec}) {
  1999.         for (PPM::UI::target_config_info($targ)->result_l) {
  2000.             push @keys, $_->[0];
  2001.             push @vals, $_->[1];
  2002.         }
  2003.         }
  2004.         $_ = ucfirst $_ for @keys;
  2005.         $o->inform("Describing target $sel ($targ):\n");
  2006.         $o->print_pairs(\@keys, \@vals);
  2007.         return 1;
  2008.     }
  2009.     }
  2010.     unless($targets) {
  2011.     $o->warn("No targets. Install a PPM target.\n");
  2012.     return;
  2013.     }
  2014.     else {
  2015.     $o->conf('target', $targets[0])
  2016.         unless $o->conf('target');
  2017.     my $i = 0;
  2018.     $o->inform("Targets:\n");
  2019.     for (@targets) {
  2020.         $o->informf(
  2021.         "%s%2d",
  2022.         $o->conf('target') eq $targets[$i] ? "*" : " ",
  2023.         $i + 1
  2024.         );
  2025.         $o->inform(". $_\n");
  2026.         $i++;
  2027.     }
  2028.     }
  2029.     1;
  2030. }
  2031.  
  2032. #============================================================================
  2033. # Query:
  2034. # query        # displays list of previous queries
  2035. # query <\d+>    # displays results of previous query
  2036. # query <terms>    # performs a new query and displays results
  2037. #============================================================================
  2038. sub smry_query { "queries installed packages" }
  2039. sub help_query { <<'END' }
  2040. query -- Query Installed Packages
  2041.   Synopsis
  2042.      query                   Displays list of previous queries
  2043.      query <number>          Displays results of previous query
  2044.      query <glob pattern>    Performs a new query using <glob pattern>
  2045.      query *                 Displays a list of all installed packages
  2046.  
  2047.   Description
  2048.     The query command displays a list of all installed packages, or a list
  2049.     based on the <glob pattern> switch. You can also check the list of past
  2050.     queries, or the results of a past query.
  2051.  
  2052.     With PPM 3.1, you can now perform much more powerful queries. The syntax
  2053.     is identical to the 'search' command, and almost all the search switches
  2054.     are also available for querying installed packages.
  2055.  
  2056.     Recall previous queries with the 'query <number>' command. PPM3 stores
  2057.     all queries from the current PPM session.
  2058.  
  2059.     Note: Depending on the value of the "case-sensitivity" setting, the
  2060.     query may or may not be case-sensitive. See "help settings" for
  2061.     instructions on setting the default case sensitivity.
  2062.  
  2063.   See Also
  2064.     search, settings
  2065. END
  2066. sub comp_query {()}
  2067. sub run_query {
  2068.     my $o = shift;
  2069.     my $query = $o->raw_args || join ' ', @_;
  2070.     trace(1, "PPM: query @_\n\tquery='$query'\n");
  2071.     my @targets = PPM::UI::target_list()->result_l;
  2072.     my $target = $o->conf('target');
  2073.     my $case = not $o->conf('case-sensitivity');
  2074.     $o->warn("You must install an installation target before using PPM.\n")
  2075.       and return unless @targets;
  2076.  
  2077.     # No args: show cached query sets
  2078.     unless ($query =~ /\S/) {
  2079.     my @query_results = $o->cache_sets('query');
  2080.     my $query_result_current = $o->cache_set_current('query');
  2081.     if (@query_results) {
  2082.         $o->inform("Query Result Sets:\n");
  2083.         my $i = 0;
  2084.         for (@query_results) {
  2085.         $o->informf("%s%2d",
  2086.                $query_result_current == $i ? "*" : " ",
  2087.                $i + 1);
  2088.         $o->inform(". $_->{query}\n");
  2089.         $i++;
  2090.         }
  2091.     }
  2092.     else {
  2093.         $o->warn("No query result sets -- provide a query term.\n");
  2094.         return;
  2095.     }
  2096.     }
  2097.  
  2098.     # Args:
  2099.     else {
  2100.     # Show specified result set 
  2101.     if ($query =~ /^\d+/) {
  2102.         my $set = int($query);
  2103.         unless (defined $o->cache_set('query', $set-1)) {
  2104.         $o->warn("No such query result set '$set'.\n");
  2105.         return;
  2106.         }
  2107.  
  2108.         $query = $o->cache_set('query', $set-1, 'query');
  2109.         $o->inform("Query Results Set $set ($query):\n");
  2110.         $o->print_formatted([$o->cache_set('query', $set-1)],
  2111.                 $o->cache_set_index('query'));
  2112.                 
  2113.         $o->cache_set_current('query', $set-1);
  2114.         $o->cache_set_index('query', -1);
  2115.     }
  2116.  
  2117.     # Query is the same a a previous query on the same target:
  2118.     # Use cached results and set them as default
  2119.     elsif (grep { $_->{query} eq $query } $o->cache_sets('query')) {
  2120.         for (my $i=0; $i<$o->cache_sets('query'); $i++) {
  2121.         if ($o->cache_set('query', $i, 'query') eq $query) {
  2122.             $o->inform("Using cached query result set ", $i+1, ".\n");
  2123.             $o->cache_set_current('query', $i);
  2124.             my $set = $o->cache_set('query');
  2125.             $o->print_formatted($set);
  2126.         }
  2127.         }
  2128.     }
  2129.  
  2130.     # Perform a new query.
  2131.     else {
  2132.         my $num = find_index($target, 1, @targets);
  2133.         $o->inform("Querying target $num (");
  2134.         if (length($target) > 30) {
  2135.         $o->inform(substr($target, 0, 30), "...");
  2136.         }
  2137.         else {
  2138.         $o->inform($target);
  2139.         }
  2140.         $o->inform(")\n");
  2141.  
  2142.         my $res = PPM::UI::query($target, $query, $case);
  2143.         unless ($res->ok) {
  2144.         $o->inform($res->msg);
  2145.         return;
  2146.         }
  2147.         my @matches = $res->result_l;
  2148.         if (@matches) {
  2149.         $o->cache_set_add('query', $query, \@matches);
  2150.         $o->cache_set_current('query', scalar($o->cache_sets('query')) - 1);
  2151.         my @set = $o->cache_set('query');
  2152.         $o->print_formatted(\@set);
  2153.         }
  2154.         else {
  2155.         $o->warn("No matches for '$query'; see 'help query'.\n");
  2156.         }
  2157.     }
  2158.     }
  2159.     1;
  2160. }
  2161.  
  2162. #============================================================================
  2163. # Properties:
  2164. # prop        # describes default installed package
  2165. # prop <\d+>    # describes numbered installed package
  2166. # prop <pkg>    # describes named installed package
  2167. # prop <url>    # describes installed package at location <url>
  2168. #============================================================================
  2169. sub smry_properties { "describes installed packages in detail" }
  2170. sub help_properties { <<'END' }
  2171. properties -- Describe Installed Packages
  2172.   Synopsis
  2173.      prop                    Describes default installed package
  2174.      prop <number>           Describes installed package <number>
  2175.      prop <range>            Describes a <range> of installed packages
  2176.      prop <package name>     Describes named installed package
  2177.      prop <url>              Describes installed package located at <url>
  2178.      prop <glob pattern>     Performs a new query using <glob pattern>
  2179.  
  2180.   Description
  2181.     The properties command is an verbose form of the describe command. In
  2182.     addition to summary information, properties will display the
  2183.     installation date and a URL showing the location of the package within
  2184.     the repository.
  2185.  
  2186.     If you specify the package as a URL, PPM determines the package name
  2187.     from the URL and searches for that.
  2188.  
  2189.     When the properties command is used with wildcard arguments, the text
  2190.     entered at the PPM prompt is passed to the query command.
  2191.  
  2192.     For example, typing 'properties libnet' will give you:
  2193.  
  2194.         ====================
  2195.             Name: libnet
  2196.          Version: 1.07.03
  2197.           Author: Graham Barr
  2198.            Title: libnet
  2199.         Abstract: Collection of Network protocol modules
  2200.         InstDate: Fri Oct  2 16:15:15 1998
  2201.         Location: http://ppm.ActiveState.com/PPM/...
  2202.         ====================
  2203.  
  2204.   See Also
  2205.     describe
  2206. END
  2207. sub comp_properties {
  2208.     my $o = shift;
  2209.     my ($word, $line, $start) = @_;
  2210.  
  2211.     # If no query results
  2212.     my $n_results = scalar $o->cache_sets('query');
  2213.     my $n_current = $o->cache_set_current('query');
  2214.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2215.     my $targ = $o->conf('target') or return ();
  2216.     my $r = PPM::UI::query($targ, '*');
  2217.     return () unless $r->ok;
  2218.     $o->cache_set_add('query', '*', $r->result);
  2219.     $o->cache_set_current('query', scalar($o->cache_sets('query')) - 1);
  2220.     }
  2221.     my @words = $o->line_parsed($line);
  2222.  
  2223.     # If the previous word isn't a number or the command, stop.
  2224.     return ()
  2225.       if ($#words > 0 and
  2226.       $words[$#words] !~ /^\d+/ and
  2227.       $start == length($line) or 
  2228.       $#words > 1);
  2229.  
  2230.     # This is the most optimistic list:
  2231.     my @results = $o->cache_set('query');
  2232.     my $npkgs = @results;
  2233.     my @compls = (1 .. $npkgs);
  2234.  
  2235.     # If the previous word is a number, return only other numbers:
  2236.     return $o->completions($word, \@compls)
  2237.       if ($words[$#words] =~ /^\d+/);
  2238.  
  2239.     # Either a number or the names of the packages
  2240.     push @compls, map { $_->name } @results;
  2241.     return $o->completions($word, \@compls);
  2242. }
  2243. sub run_properties {
  2244.     my $o = shift;
  2245.     my @args = @_;
  2246.     my $args = $args[0];
  2247.     trace(1, "PPM: properties @args\n");
  2248.  
  2249.     # Check for anything that looks like a query. If it does, send it
  2250.     # to query instead.
  2251.     my $query = $o->raw_args || join ' ', @args;
  2252.     $query ||= '';
  2253.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  2254.     $o->inform("Wildcards detected; using 'query' instead.\n");
  2255.     return $o->run('query', @_);
  2256.     }
  2257.     
  2258.     # No Args: describes current index of current result set, or 1.
  2259.     my $n_results = $o->cache_sets('query');
  2260.     my $n_current = $o->cache_set_current('query');
  2261.     my $ind = $o->cache_set_index('query');
  2262.     unless (@args) {
  2263.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2264.         $o->inform("No query results to describe -- " . 
  2265.           "use 'query' to find a package.\n");
  2266.         return;
  2267.     }
  2268.     else {
  2269.         my @results = $o->cache_set('query');
  2270.         my $npkgs = @results;
  2271.         $o->inform("$SEP\n");
  2272.         if (bounded(0, $ind, $npkgs-1)) {
  2273.         my $n = $ind + 1;
  2274.         $o->inform("Package $n:\n");
  2275.         $o->describe_pkg($o->cache_entry('query', $ind));
  2276.         }
  2277.         else {
  2278.         $o->inform("Package 1:\n");
  2279.         $o->describe_pkg($results[0]);
  2280.         $o->cache_set_index('query', 0);
  2281.         }
  2282.         $o->inform("$SEP\n");
  2283.     }
  2284.     }
  2285.  
  2286.     # Args provided
  2287.     else {
  2288.  
  2289.     # Describe a particular number:
  2290.     if (my @r = parse_range(@args)) {
  2291.         unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2292.         $o->inform("No query results to describe -- " . 
  2293.           "use 'query' to find a package.\n");
  2294.         return;
  2295.         }
  2296.         else {
  2297.         for my $n (@r) {
  2298.             my @results = $o->cache_set('query');
  2299.             my $npkgs = @results;
  2300.             $o->inform("$SEP\n");
  2301.             if (bounded(1, $n, $npkgs)) {
  2302.             $o->inform("Package $n:\n");
  2303.             $o->cache_set_index('query', $n-1);
  2304.             my $old = $o->cache_entry('query');
  2305.             my $prop =
  2306.               PPM::UI::properties($o->conf('target'), $old->name);
  2307.             unless ($prop->is_success) {
  2308.                 $o->warn($prop->msg);
  2309.                 next unless $prop->ok;
  2310.             }
  2311.             my ($pkg, $idate, $loc) = $prop->result_l;
  2312.             $o->describe_pkg($pkg,
  2313.                      [qw(InstDate Location)],
  2314.                      [$idate, $loc],
  2315.                     );
  2316.             }
  2317.             else {
  2318.             $o->inform("No such package $n in result set.\n");
  2319.             }
  2320.         }
  2321.         $o->inform("$SEP\n");
  2322.         }
  2323.     }
  2324.  
  2325.     # Query a particular package
  2326.     else {
  2327.         if ($o->conf('target')) {
  2328.         my $prop =
  2329.           PPM::UI::properties($o->conf('target'), $args);
  2330.         unless ($prop->is_success) {
  2331.             $o->warn($prop->msg);
  2332.             return unless $prop->ok;
  2333.         }
  2334.         my ($pkg, $idate, $loc) = $prop->result_l;
  2335.         my ($s, $index) = $o->cache_find('query', $args);
  2336.         $o->inform("$SEP\n") if $pkg;
  2337.         $o->describe_pkg($pkg,
  2338.                  [qw(InstDate Location)],
  2339.                  [$idate, $loc],
  2340.                 )
  2341.           if $pkg;
  2342.         $o->inform("$SEP\n") if $pkg;
  2343.         if ($index >= 0) {
  2344.             $o->cache_set_current('query', $s);
  2345.             $o->cache_set_index('query', $index);
  2346.         }
  2347.         elsif ($pkg) {
  2348.             $o->cache_set_add('query', $args[0], [$pkg]);
  2349.             my $last = $o->cache_sets('query') - 1;
  2350.             $o->cache_set_current('query', $last);
  2351.             $o->cache_set_index('query', 0);
  2352.         }
  2353.         $o->warn("Package '$args' not found; 'query' for it first.\n")
  2354.           and return unless $pkg;
  2355.         }
  2356.         else {
  2357.         # XXX: Change this output.
  2358.         $o->warn(
  2359.             "There are no targets installed.\n"
  2360.         );
  2361.         return;
  2362.         }
  2363.     }
  2364.     }
  2365.     1;
  2366. }
  2367.  
  2368. #============================================================================
  2369. # Uninstall:
  2370. # uninst    # removes default installed package
  2371. # uninst <\d+>    # removes specified package
  2372. # uninst <pkg>    # removes specified package
  2373. # uninst <url>    # removes the package located at <url>
  2374. #============================================================================
  2375. sub smry_uninstall { "uninstalls packages" }
  2376. sub help_uninstall { <<'END' }
  2377. remove, uninstall -- Uninstalls Installed Packages
  2378.   Synopsis
  2379.      remove              Deletes default installed package
  2380.      remove <number>     Deletes installed package <number>
  2381.      remove <range>      Deletes a <range> of installed packages
  2382.      remove <name>       Deletes a packages by a specific name
  2383.      remove <url>        Deletes the package located at <url>
  2384.  
  2385.   Description
  2386.     The remove and uninstall commands function identically. They are used to
  2387.     delete packages from the current target (specified using the target
  2388.     command). If profile tracking is enabled, (see 'help profile') the
  2389.     current PPM profile on ASPN will be updated.
  2390.  
  2391.     Packages can be removed by package name, by their numerical listing, or
  2392.     by specifying a URL to a PPD file. For example:
  2393.  
  2394.         remove XML-DOM
  2395.  
  2396.     will delete the XML-DOM package from the target.
  2397.  
  2398.     To remove package by number:
  2399.  
  2400.         remove 6
  2401.  
  2402.     and the sixth package in your current query will be removed. If no
  2403.     queries have been run in the current PPM session, you will be prompted
  2404.     to use a query to find a package before deleting it. Remember that
  2405.     removing packages clears all previous query requests, since the
  2406.     numerical sequence stored in any query will no longer be true once
  2407.     package(s) have been removed.
  2408.  
  2409.     Packages can also be removed in groups. For example:
  2410.  
  2411.         remove 4-7
  2412.  
  2413.     will delete packages 4, 5, 6, and 7 from your target. You can also skip
  2414.     packages:
  2415.  
  2416.         remove 3-5, 7
  2417.  
  2418.     this will delete packages 3, 4, 5 and 7, but will leave 6 intact.
  2419.     Remember to run a new query whenever you remove a package from your
  2420.     target.
  2421.  
  2422.     If you specify the package as a URL, PPM determines the package name
  2423.     from the URL and removes that.
  2424.  
  2425.     Please note that wildcards like "*" or "?" cannot be used with the
  2426.     remove command.
  2427.  
  2428.   See Also
  2429.     profile
  2430. END
  2431. sub comp_uninstall { goto &comp_properties; }
  2432. sub run_uninstall {
  2433.     my $o = shift;
  2434.     my @args = @_;
  2435.     trace(1, "PPM: uninstall @args\n");
  2436.  
  2437.     # Get the force option:
  2438.     my ($force);
  2439.     {
  2440.     local @ARGV = @args;
  2441.     GetOptions(
  2442.         'force!' => \$force,
  2443.     );
  2444.     @args = @ARGV;
  2445.     }
  2446.     
  2447.     my $args = $args[0];
  2448.  
  2449.     # No Args -- removes default package
  2450.     my $n_results = $o->cache_sets('query');
  2451.     my $n_current = $o->cache_set_current('query');
  2452.     my $ind = $o->cache_set_index('query');
  2453.     unless (@args) {
  2454.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2455.         $o->warn("No query results to uninstall -- " . 
  2456.           "use 'query' to find a package.\n");
  2457.         return;
  2458.     }
  2459.     else {
  2460.         my @results = $o->cache_set('query');
  2461.         if (bounded(0, $ind, $#results)) {
  2462.         my $n = $ind + 1;
  2463.         $o->inform("Package $n:\n");
  2464.         $o->remove_pkg($o->cache_entry('query', $ind)->name, $force);
  2465.         }
  2466.         else {
  2467.         $o->inform("Package 1:\n");
  2468.         $o->remove_pkg($o->cache_entry('query', 0)->name, $force);
  2469.         }
  2470.     }
  2471.     }
  2472.  
  2473.     # Args provided
  2474.     else {
  2475.     # Uninstall a particular number:
  2476.     if (my @r = parse_range(@args)) {
  2477.         unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2478.         $o->warn("No query results to uninstall -- " . 
  2479.           "use 'query' to find a package.\n");
  2480.         return;
  2481.         }
  2482.         else {
  2483.         my @results = $o->cache_set('query');
  2484.         my $npkgs = @results;
  2485.         my $ok = 0;
  2486.         for my $n (@r) {
  2487.             if (bounded(1, $n, $npkgs)) {
  2488.             $o->inform("Package $n:\n");
  2489.             $ok |=
  2490.               $o->remove_pkg($o->cache_entry('query', $n-1)->name,
  2491.                      $force, 1);
  2492.             }
  2493.             else {
  2494.             $o->warn("No such package $n in result set.\n");
  2495.             }
  2496.         }
  2497.         $o->cache_clear('query') if $ok;
  2498.         }
  2499.     }
  2500.  
  2501.     # Uninstall a particular package
  2502.     else {
  2503.         if ($o->conf('target')) {
  2504.         $o->remove_pkg($_, $force) for @args;
  2505.         }
  2506.         else {
  2507.         print
  2508.           "No targets -- use 'rep add' to add a target.\n";
  2509.         return;
  2510.         }
  2511.     }
  2512.     }
  2513.     1;
  2514. }
  2515. sub alias_uninstall { qw(remove) }
  2516.  
  2517. #============================================================================
  2518. # Settings:
  2519. #============================================================================
  2520. my (%lib_keys, @ui_keys);
  2521. my (@path_keys, @boolean_keys, @integer_keys);
  2522. my (%cache_clear_keys);
  2523. BEGIN {
  2524.     %lib_keys = ('download-chunksize' => 'downloadbytes',
  2525.         'tempdir' => 'tempdir',
  2526.         'rebuild-html' => 'rebuildhtml',
  2527.         'trace-file' => 'tracefile',
  2528.         'trace-level' => 'tracelvl',
  2529.         'profile-track' => 'profile_enable',
  2530.         );
  2531.     @ui_keys = qw(
  2532.     case-sensitivity
  2533.     pager
  2534.     fields
  2535.     follow-install
  2536.     force-install
  2537.     prompt-context
  2538.     prompt-slotsize
  2539.     prompt-verbose
  2540.     sort-field
  2541.     verbose-startup
  2542.  
  2543.     install-verbose
  2544.     upgrade-verbose
  2545.     remove-verbose
  2546.     );
  2547.     @boolean_keys = qw(case-sensitivity force-install follow-install
  2548.                prompt-context prompt-verbose profile-track
  2549.                verbose-startup install-verbose upgrade-verbose
  2550.                remove-verbose rebuild-html
  2551.               );
  2552.     @integer_keys = qw(download-chunksize prompt-slotsize trace-level);
  2553.     @path_keys = qw(tempdir pager trace-file);
  2554.     @cache_clear_keys{qw/
  2555.     case-sensitivity
  2556.     /} = ();
  2557. }
  2558. sub settings_getkeys {
  2559.     my $o = shift;
  2560.     my @keys = @ui_keys;
  2561.     push @keys, keys %lib_keys;
  2562.     @keys;
  2563. }
  2564. sub settings_getvals {
  2565.     my $o = shift;
  2566.     my @vals;
  2567.     push @vals, $o->settings_getkey($_) for $o->settings_getkeys;
  2568.     @vals;
  2569. }
  2570.  
  2571. sub conf {
  2572.     my $o   = shift;
  2573.     my $key = shift;
  2574.     my $val = shift;
  2575.     my $un  = shift;
  2576.     return $o->settings_setkey($key, $val, $un) if defined $val;
  2577.     return $o->settings_getkey($key);
  2578. }
  2579.  
  2580. sub settings_getkey {
  2581.     my $o = shift;
  2582.     my $key = shift;
  2583.     return PPM::UI::config_get($lib_keys{$key})->result if $lib_keys{$key};
  2584.     return $o->{SHELL}{conf}{DATA}{$key};
  2585. }
  2586. sub settings_setkey {
  2587.     my $o = shift;
  2588.     my ($key, $val, $un) = @_;
  2589.     if (grep { $key eq $_ } @boolean_keys) {
  2590.     $val = 0 if $un;
  2591.     unless ($val =~ /^\d+$/ && ($val == 0 || $val == 1)) {
  2592.         $o->warn(<<END);
  2593. Setting '$key' must be boolean: '0' or '1'. See 'help settings'.
  2594. END
  2595.         return;
  2596.     }
  2597.     }
  2598.     elsif (grep { $key eq $_ } @integer_keys) {
  2599.     $val = 0 if $un;
  2600.     unless ($val =~ /^\d+$/) {
  2601.         $o->warn(<<END);
  2602. Setting '$key' must be numeric. See 'help settings'.
  2603. END
  2604.         return;
  2605.     }
  2606.     }
  2607.     elsif ($key eq 'sort-field') {
  2608.     $val = 'name' if $un;
  2609.     my @fields = sort_fields();
  2610.     unless (grep { lc($val) eq $_ } @fields) {
  2611.         $o->warn(<<END);
  2612. Error setting '$key' to '$val': should be one of:
  2613. @fields.
  2614. END
  2615.         return;
  2616.     }
  2617.     else {
  2618.         $val = lc($val);
  2619.         $o->cache_set_index('search', -1); # invalidates current indices.
  2620.         $o->cache_set_index('query', -1);
  2621.     }
  2622.     }
  2623.     elsif ($key eq 'fields') {
  2624.     $val = 'name version abstract' if $un;
  2625.     my @fields = sort_fields();
  2626.     my @vals = split ' ', $val;
  2627.     for my $v (@vals) {
  2628.         unless (grep { lc $v eq lc $_ } @fields) {
  2629.         $o->warn(<<END);
  2630. Error adding field '$v': should be one of:
  2631. @fields.
  2632. END
  2633.         return;
  2634.         }
  2635.     }
  2636.     $val = lc $val;
  2637.     }
  2638.  
  2639.     if ($un and $key eq 'tempdir') {
  2640.     $o->warn("Can't unset 'tempdir': use 'set' instead.\n");
  2641.     return;
  2642.     }
  2643.  
  2644.     # Check for any cache-clearing that needs to happen:
  2645.     if (exists $cache_clear_keys{$key}) {
  2646.     $o->cache_clear('search');
  2647.     $o->cache_clear('query');
  2648.     }
  2649.  
  2650.     if ($lib_keys{$key}) { PPM::UI::config_set($lib_keys{$key}, $val) }
  2651.     else {
  2652.     $o->{SHELL}{conf}{DATA}{$key} = $val;
  2653.     $o->{SHELL}{conf}->save;
  2654.     }
  2655.     $o->inform(<<END);
  2656. Setting '$key' set to '$val'.
  2657. END
  2658. }
  2659.  
  2660. sub smry_settings { "view or set PPM options" }
  2661. sub help_settings { <<'END' }
  2662. settings -- View or Set PPM Settings
  2663.   Synopsis
  2664.      set                 Displays current settings
  2665.      set <name>          Displays the current setting of the given <name>
  2666.      set <name> <value>  Sets <name> to <value>
  2667.      unset <name>        Sets <name> to a "false" value: '0' for boolean
  2668.                          settings, '' for others.
  2669.  
  2670.   Description
  2671.     The settings command is used to configure the default PPM environment.
  2672.     Settings such as the number of lines displayed per page,
  2673.     case-sensitivity, and the log file are configured using the settings
  2674.     command.
  2675.  
  2676.     Setting names may be abbreviated to uniqueness. For example, instead of
  2677.     typing 'case-sensitivity', you may type 'case'.
  2678.  
  2679.     Available settings:
  2680.  
  2681.      NAME                VALUE           DESCRIPTION
  2682.      case-sensitivity    1 or 0      If 1, searches and queries are
  2683.                                      case-sensitive.
  2684.  
  2685.      download-chunksize  integer     If this is set to a positive,
  2686.                                      non-zero integer, PPM updates the
  2687.                                      status after "integer" of bytes
  2688.                                      transferred during an install or
  2689.                                      upgrade.
  2690.  
  2691.      fields              fields      A space-separated list of fields to 
  2692.                                      display in the search results. Valid
  2693.                                      fields are:
  2694.  
  2695.                                        ABSTRACT
  2696.                                        AUTHOR
  2697.                                        NAME
  2698.                                        REPOSITORY
  2699.                                        TITLE
  2700.                                        VERSION
  2701.  
  2702.                                      Usually, NAME and TITLE have the same
  2703.                                      content.
  2704.  
  2705.      follow-install      1 or 0      See 'help install' for details.
  2706.  
  2707.      force-install       1 or 0      See 'help install' for details.
  2708.  
  2709.      install-verbose     1 or 0      If 0, suppresses most output when
  2710.                                      installing packages. If 1, PPM prints
  2711.                                      each file as it is installed.
  2712.  
  2713.      pager               path        The path to an external pager program
  2714.                                      used to page long displays. If blank,
  2715.                                      or set to 'internal', the internal
  2716.                                      pager is used. If 'none', paging
  2717.                                      is disabled.
  2718.  
  2719.      profile-track       1 or 0      If 1, PPM arranges to have the 
  2720.                                      ASPN server track your PPM profile. 
  2721.                                      This means that every time your install
  2722.                                      or remove a package, your profile is
  2723.                                      updated on the server. If 0, you must
  2724.                                      manually save your profile using
  2725.                                      'profile save'.
  2726.  
  2727.      prompt-context      1 or 0      If 1, enables the prompt to change
  2728.                                      based on the current state of PPM, i.e
  2729.                                      showing current target, query, etc.
  2730.  
  2731.      prompt-slotsize     integer     If prompt-verbose is 1, this defines
  2732.                                      the width of each slot in the prompt.
  2733.                                      For instance, 4 means to use 4 
  2734.                                      character-wide slots.
  2735.  
  2736.      prompt-verbose      1 or 0      If 0, uses numbers to represent the
  2737.                                      context in the prompt; much shorter.
  2738.                                      If prompt-context is set to 0, there
  2739.                                      will be no visible difference in the
  2740.                                      'prompt-verbose' settings.
  2741.  
  2742.      rebuild-html        1 or 0      If 0, suppresses regeneration of HTML
  2743.                                      documentation when packages are
  2744.                                      installed. If 1, enables HTML to be
  2745.                                      generated from POD documentation.
  2746.                                      Enabling this option may slow down
  2747.                                      package installation.
  2748.  
  2749.      remove-verbose      1 or 0      If 0, suppresses most output when
  2750.                                      removing packages. If 1, prints the
  2751.                                      name of each file as it is removed.
  2752.  
  2753.      sort-field          field       The field by which to sort search and
  2754.                                      query results. Valid fields are
  2755.                                      ABSTRACT, AUTHOR, NAME, TITLE
  2756.                                      and VERSION.
  2757.  
  2758.      tempdir             path        A temporary directory into which
  2759.                                      packages are downloaded and expanded
  2760.                                      during 'install' and 'upgrade'.
  2761.  
  2762.      trace-file          path        A file to which PPM will write tracing
  2763.                                      information.
  2764.  
  2765.      trace-level         integer     If 0 or negative, tracing is disabled.
  2766.                                      Positive, non-zero integers result in
  2767.                                      tracing information being written to
  2768.                                      'trace-file'. Higher settings of
  2769.                                      'trace-level' result in more trace
  2770.                                      information.
  2771.  
  2772.      upgrade-verbose     1 or 0      If 0, suppresses most output when
  2773.                                      upgrading packages. If 1, prints the
  2774.                                      name of each file as it is upgraded.
  2775.  
  2776.     For information about migrating options used by previous versions of
  2777.     PPM, see 'help ppm_migration'.
  2778.  
  2779.     When you assign a value to a setting, PPM saves the configuration.
  2780.     Therefore, setting values persist across sessions.
  2781. END
  2782. sub comp_settings {
  2783.     my $o = shift;
  2784.     my ($word, $line, $start) = @_;
  2785.     my @words = $o->line_parsed($line);
  2786.  
  2787.     # To please the users of Bash, we'll allow 'set foo=bar' to work as well,
  2788.     # since it's really easy to do:
  2789.     if (defined $words[1] and $words[1] =~ /=/ and not defined $words[2]) {
  2790.     my @kv = split '=', $words[1];
  2791.     splice(@words, 1, 1, @kv);
  2792.     }
  2793.     my $words = @words;
  2794.     my @compls;
  2795.  
  2796.     # return the keys when we're completing the second word
  2797.     if ($words == 1 or $words == 2 and $start != length($line)) {
  2798.     @compls = $o->settings_getkeys();
  2799.     return $o->completions($word, \@compls);
  2800.     }
  2801.  
  2802.     # Return no completions for 'unset'.
  2803.     return () if matches($o->{API}{cmd}{run}{name}, 'u|nset');
  2804.  
  2805.     # provide intelligent completion for arguments:
  2806.     if ($words ==2 or $words == 3 and $start != length($line)) {
  2807.     # Completion for boolean values:
  2808.     my @bool = $o->completions($words[1], \@boolean_keys);
  2809.     my @path = $o->completions($words[1], \@path_keys);
  2810.     if (@bool == 1) {
  2811.         return $o->completions($word, [0, 1]);
  2812.     }
  2813.     elsif (@path == 1) {
  2814.         @compls = readline::rl_filename_list($word);
  2815.         return $o->completions($word, \@compls);
  2816.     }
  2817.     elsif (matches($words[1], 's|ort-field')) {
  2818.         @compls = sort_fields();
  2819.         return $o->completions(lc($word), \@compls);
  2820.     }
  2821.     }
  2822.  
  2823.     # Don't complete for anything else.
  2824.     ()
  2825. }
  2826. sub run_settings {
  2827.     my $o = shift;
  2828.     my @args = @_;
  2829.     my $key = $args[0];
  2830.     my $val = $args[1];
  2831.  
  2832.     # To please the users of Bash, we'll allow 'set foo=bar' to work as well,
  2833.     # since it's really easy to do:
  2834.     if (defined $key and $key =~ /=/ and not defined $val) {
  2835.     ($key, $val) = split '=', $key;
  2836.     }
  2837.  
  2838.     trace(1, "PPM: settings @args\n");
  2839.     my $unset = matches($o->{API}{cmd}{run}{name}, 'u|nset');
  2840.     my @stuff = $o->completions($key, [$o->settings_getkeys()])
  2841.       if $key;
  2842.     my $fullkey = $stuff[0] if @stuff == 1;
  2843.     if (defined $key and defined $val) {
  2844.     # validate the key:
  2845.     unless ($fullkey) {
  2846.         $key = '' unless defined $key;
  2847.         $o->warn("Unknown or ambiguous setting '$key'. See 'help settings'.\n");
  2848.         return;
  2849.     }
  2850.     $o->conf($fullkey, $val, $unset);
  2851.     }
  2852.     elsif (defined $key) {
  2853.     unless ($fullkey) {
  2854.         $key = '' unless defined $key;
  2855.         $o->warn("Unknown or ambiguous setting '$key'. See 'help settings'.\n");
  2856.         return;
  2857.     }
  2858.     if ($unset) {
  2859.         $o->conf($fullkey, '', $unset);
  2860.     }
  2861.     else {
  2862.         my $val = $o->conf($fullkey);
  2863.         $o->print_pairs([$fullkey], [$val]);
  2864.     }
  2865.     }
  2866.     else {
  2867.     my (@keys, @vals);
  2868.     @keys = $o->settings_getkeys();
  2869.     @vals = $o->settings_getvals();
  2870.     my %k;
  2871.     @k{@keys} = @vals;
  2872.     @keys = sort keys %k;
  2873.     @vals = map { $k{$_} } @keys;
  2874.     $o->print_pairs(\@keys, \@vals);
  2875.     }
  2876. }
  2877. sub alias_settings { qw(unset) }
  2878.  
  2879. sub help_help { <<'END' }
  2880. help -- General help, or help on specific commands.
  2881.   Synopsis
  2882.      help                Lists available commands and help topics
  2883.      help <command>      Lists detailed help about a specific command
  2884.  
  2885.   Description
  2886.     The help command provides a brief description of the commands available
  2887.     within PPM. For help on a specific command, enter help followed by the
  2888.     command name. For example, enter help settings or help set for a
  2889.     detailed description of the settings command.
  2890.  
  2891.     There are some extra help topics built into PPM. They can be accessed
  2892.     within the PPM environment as follows:
  2893.  
  2894.       help ppm_migration
  2895.  
  2896.     shows more details about the changes from previous versions of PPM
  2897.  
  2898.       help quickstart
  2899.  
  2900.     an easy-to-follow guide to getting started with PPM
  2901.  
  2902.       help prompt
  2903.  
  2904.     provides a detailed explanation about the PPM prompt
  2905. END
  2906.  
  2907. #============================================================================
  2908. # Version:
  2909. #============================================================================
  2910. sub smry_version { "displays the PPM version ($VERSION)" }
  2911. sub help_version { <<'END' }
  2912. version -- print the name and version of PPM.
  2913.     Prints the name and version of PPM3.
  2914. END
  2915. sub comp_version {()}
  2916. sub run_version {
  2917.     my $o = shift;
  2918.     if ($o->mode eq 'SHELL') {
  2919.     $o->inform("$NAME version $VERSION\n");
  2920.     }
  2921.     else {
  2922.     $o->inform("$SHORT_NAME $VERSION\n");
  2923.     }
  2924.     1;
  2925. }
  2926.  
  2927. #============================================================================
  2928. # Exit:
  2929. #============================================================================
  2930. sub help_exit { <<'END' }
  2931. exit, q, quit -- Exit the program
  2932.   Synopsis
  2933.      exit                Exit
  2934.      q                   Exit
  2935.      quit                Exit
  2936.  
  2937.   Description
  2938.     When you leave the PPM environment, the current settings are saved.
  2939. END
  2940. sub comp_exit {
  2941.     my $o = shift;
  2942.     return &comp_query
  2943.     if $o->{API}{cmd}{run}{name} eq 'q' and @_;
  2944.     ();
  2945. }
  2946. sub run_exit {
  2947.     my $o = shift;
  2948.     # Special case: 'q' with no arguments should mean 'quit', but 'q' with
  2949.     # arguments should mean 'query'.
  2950.     if ($o->{API}{cmd}{run}{name} eq 'q' and @_) {
  2951.     return $o->run('query', @_);
  2952.     }
  2953.     $o->stoploop;
  2954. }
  2955. sub alias_exit { qw(quit q) }
  2956.  
  2957. #============================================================================
  2958. # Upgrade
  2959. # upgrade    # lists upgrades available
  2960. # upgrade <\d+> # lists upgrades for specified package
  2961. # upgrade<pkg>    # lists upgrades for named package
  2962. #============================================================================
  2963. sub smry_upgrade { "shows availables upgrades for installed packages" }
  2964. sub help_upgrade { <<'END' }
  2965. upgrade -- List or install available upgrades
  2966.   Synopsis
  2967.      upgrade [*]         Lists upgrades available for all installed packages
  2968.      upgrade <number>    Lists upgrades for installed package <number>
  2969.      upgrade <range>     Lists upgrades for a <range> of installed packages
  2970.      upgrade <package>   Lists upgrades for the named <package>
  2971.  
  2972.   Description
  2973.     The upgrade command lists package upgrades that are available on the
  2974.     active repositories for packages installed on your system. To install
  2975.     available upgrades, use the '--install' option.
  2976.  
  2977.     If profile tracking is enabled, (see 'help profile'), your profile will
  2978.     be updated to reflect changes to any packages which are upgraded.
  2979.  
  2980.     There are several modifiers to the upgrade command:
  2981.  
  2982.     -install
  2983.         Installs, rather than lists, available upgrades
  2984.  
  2985.     -precious
  2986.         Allows upgrading of "precious" packages
  2987.  
  2988.     -force
  2989.         See 'help install'
  2990.  
  2991.     -follow
  2992.         See 'help install'
  2993.  
  2994.     By default, 'upgrade' typed by itself only lists the available upgrades.
  2995.     To actually install all available upgrades, enter
  2996.  
  2997.         upgrade -install
  2998.  
  2999.     To enable upgrading "precious" packages, enter
  3000.  
  3001.         upgrade -install -precious
  3002.  
  3003.   See Also
  3004.     profile
  3005. END
  3006. sub comp_upgrade { goto &comp_properties; }
  3007. sub run_upgrade {
  3008.     my $o = shift;
  3009.     my @args = @_;
  3010.     trace(1, "PPM: upgrade @args\n");
  3011.  
  3012.     # Get options:
  3013.     my %opts = (
  3014.     install => 0,
  3015.     doprecious => 0,
  3016.     dryrun => 0,
  3017.     force => $o->conf('force-install'),
  3018.     follow => $o->conf('follow-install'),
  3019.     );
  3020.     {
  3021.     local @ARGV = @args;
  3022.     GetOptions(install => \$opts{install},
  3023.            precious => \$opts{doprecious},
  3024.            'force!' => \$opts{force},
  3025.            'follow!' => \$opts{follow},
  3026.            dryrun => \$opts{dryrun},
  3027.           );
  3028.     @args = @ARGV;
  3029.     }
  3030.  
  3031.     my $rlist = [$o->reps_on];
  3032.     my $targ  = $o->conf('target');
  3033.     my @pkgs;
  3034.  
  3035.     # Allow 'upgrade *';
  3036.     @args = grep { $_ ne '*' } @args;
  3037.  
  3038.     # List upgrades for a particular package
  3039.     if (@args) {
  3040.     my $pkg = $args[0];
  3041.     my @n = parse_range($o->raw_args);
  3042.     for my $n (@n) {
  3043.         my $ppd = $o->cache_entry('query', $n-1);
  3044.         unless($ppd) {
  3045.         $o->warn("No such query result '$pkg' in result set.\n");
  3046.         return;
  3047.         }
  3048.         else {
  3049.         push @pkgs, $ppd;
  3050.         }
  3051.     }
  3052.  
  3053.     # The name of the package:
  3054.     unless (@n) {
  3055.         my $ppd = PPM::UI::properties($o->conf('target'), $pkg);
  3056.         unless ($ppd->is_success) {
  3057.         $o->warn($ppd->msg);
  3058.         return unless $ppd->ok;
  3059.         }
  3060.         my $real_ppd = ($ppd->result_l)[0];
  3061.         push @pkgs, $real_ppd;
  3062.     }
  3063.     }
  3064.     # List upgrades for all packages
  3065.     else {
  3066.     @pkgs = PPM::UI::query($targ, '*', 0)->result_l;
  3067.     @pkgs = $o->sort_pkgs($o->conf('sort-field'), @pkgs);
  3068.     }
  3069.  
  3070.     my $verify = PPM::UI::verify_pkgs($rlist, $targ, @pkgs);
  3071.     unless ($verify->is_success) {
  3072.     $o->error("Error verifying packages: ", $verify->msg_raw, "\n");
  3073.     return;
  3074.     }
  3075.     my %bypackage;
  3076.     for my $result ($verify->result_l) {
  3077.     next unless $result->is_success; # ignore unfound packages
  3078.     my ($uptodate, $server_pkg, $inst_pkg, $b, $p) = $result->result_l;
  3079.     my $name = $server_pkg->name;
  3080.     my $nver = $server_pkg->version;
  3081.     my $over = $inst_pkg->version;
  3082.     my $repo = $server_pkg->repository->name;
  3083.     $bypackage{$name}{$repo} = {
  3084.         uptodate => $uptodate,
  3085.         oldver => $over,
  3086.         newver => $nver,
  3087.         repo => $repo,
  3088.         bundled => $b,
  3089.         precious => $p,
  3090.         pkg => $server_pkg,
  3091.     };
  3092.     }
  3093.     for my $pkg (sort keys %bypackage) {
  3094.     my $default;
  3095.     my @updates;
  3096.     my $p = $bypackage{$pkg};
  3097.     for my $rep (sort { $p->{$b}{newver} cmp $p->{$a}{newver} } keys %$p) {
  3098.         my $tmp = $default = $p->{$rep};
  3099.         push @updates, [@$tmp{qw(oldver newver repo)}] unless $tmp->{uptodate};
  3100.     }
  3101.     my $upgrade = $opts{install} ? 1 : 0;
  3102.         for (@updates) {
  3103.         $o->inform("$pkg $_->[0]: new version $_->[1] available in $_->[2]\n");
  3104.     }
  3105.     unless (@updates) {
  3106.         $o->inform("$pkg $default->{oldver}: up to date.\n");
  3107.         $upgrade &= $opts{force};
  3108.     }
  3109.     if ($upgrade) {
  3110.         my @k = keys %$p;
  3111.         my $ask = (@updates > 1 or @k > 1 and !@updates);
  3112.         if ($ask) {
  3113.         # Which one do they want to install?
  3114.         $o->inform(<<MANY);
  3115.  
  3116.    Note: $pkg version $default->{oldver} is available from more than one place.
  3117.    Which repository would you like to upgrade from?
  3118.  
  3119. MANY
  3120.         my @repos = map { $_->[2] } @updates;
  3121.         $o->print_pairs([ 1 .. @repos ], \@repos, '. ');
  3122.         $o->inform("\n");
  3123.         my $rep = $o->prompt(
  3124.             "Repository? [$default->{repo}] ",
  3125.             $default->{repo},
  3126.             [ 1 .. @repos, @repos ],
  3127.         );
  3128.         $rep = $repos[$rep - 1] if $rep =~ /^\d+$/;
  3129.         $default = $p->{$rep};
  3130.         }
  3131.         elsif (!@updates) {
  3132.         ($default) = values %$p;
  3133.         }
  3134.         if (not $default->{precious} or $default->{precious} && $opts{doprecious}) {
  3135.         $o->upgrade_pkg($default->{pkg}, \%opts);
  3136.         }
  3137.         else {
  3138.         $o->warn(<<END);
  3139. Use '-precious' to force precious packages to be upgraded.
  3140. END
  3141.         }
  3142.     }
  3143.     }
  3144.     1;
  3145. }
  3146.  
  3147. #============================================================================
  3148. # Profile:
  3149. # profile        # lists the profiles available on the repository
  3150. # profile N        # switches profiles
  3151. # profile add "name"    # adds a new profile
  3152. # profile delete N    # deletes the given profile
  3153. # profile describe N    # describes the given profile
  3154. # profile save        # saves the current state to the current profile
  3155. # profile restore    # restores the current profile
  3156. # profile rename    # renames the given profile
  3157. #============================================================================
  3158. sub smry_profiles { "manage PPM profiles" }
  3159. sub help_profiles { <<'END' }
  3160. profile -- Manage PPM Profiles
  3161.   Synopsis
  3162.      profile                     Lists profiles available on the repository
  3163.      profile <num>               Switches to the given profile
  3164.      profile add <name>          Creates a new profile on the repository
  3165.      profile delete <name or num>
  3166.                                  Deletes the given profile
  3167.      profile describe [name or num]
  3168.                                  Describes the current or given profile
  3169.      profile save                Saves the client state to the current profile
  3170.      profile restore             Restores the current profile
  3171.      profile rename <name or num> <name>
  3172.                                  Renames the given profile to <name>
  3173.  
  3174.   Description
  3175.     Profiles store information about packages that are installed on your
  3176.     system. If the 'profile-track' setting is enabled, your ASPN Profile
  3177.     will be updated with information about installed packages. Profiles
  3178.     allow you to easily migrate, reinstall, upgrade or restore PPM packages
  3179.     in one or more locations.
  3180.  
  3181.     To use profiles, you must have a license for ASPN. For license
  3182.     information, see http://www.ActiveState.com/ASPN/About Disable profile
  3183.     tracking by setting 'profile-track=0'.
  3184. END
  3185. sub comp_profiles {
  3186.     my $o = shift;
  3187.     my ($word, $line, $start) = @_;
  3188.     my @words = $o->line_parsed($line);
  3189.     my $words = scalar @words;
  3190.     my @profs = PPM::UI::profile_list();
  3191.     my @cmds = ('add', 'delete', 'describe', 'save', 'restore', 'rename');
  3192.  
  3193.     if ($words == 1 or $words == 2 and $start != length($line)) {
  3194.     my @compls = (@cmds, 1 .. scalar @profs);
  3195.     return $o->completions($word, \@compls);
  3196.     }
  3197.     if ($words == 2 or $words == 3 and $start != length($line)) {
  3198.     return ()
  3199.       if ($o->completions($words[1], [qw(add save restore)])==1);
  3200.     return $o->completions($word, [1 .. scalar @profs])
  3201.       if ($o->completions($words[1], [qw(delete describe rename)])==1);
  3202.     }
  3203.     ();
  3204. }
  3205. sub run_profiles {
  3206.     my $o = shift;
  3207.     my @args = @_;
  3208.     trace(1, "PPM: profile @args\n");
  3209.  
  3210.     my $ok = PPM::UI::profile_list();
  3211.     unless ($ok->is_success) {
  3212.     $o->warn($ok->msg);
  3213.     return unless $ok->ok;
  3214.     }
  3215.     my @profiles = dictsort $ok->result_l;
  3216.     $ok = PPM::UI::profile_get();
  3217.     unless ($ok->is_success) {
  3218.     $o->warn($ok->msg);
  3219.     return unless $ok->ok;
  3220.     }
  3221.     my $profile = $ok->result;
  3222.     my $which = find_index($profile, 0, @profiles);
  3223.     if ($which < 0 and @profiles) {
  3224.     $profile = $profiles[0];
  3225.     PPM::UI::profile_set($profile);
  3226.     }
  3227.  
  3228.     if (@args) {
  3229.     # Switch to profile N:
  3230.     if ($args[0] =~ /^\d+$/) {
  3231.         my $num = $args[0];
  3232.         if (bounded(1, $num, scalar @profiles)) {
  3233.         my $profile = $profiles[$num-1];
  3234.         PPM::UI::profile_set($profile);
  3235.         }
  3236.         else {
  3237.         $o->warn("No such profile number '$num'.\n");
  3238.         return;
  3239.         }
  3240.     }
  3241.  
  3242.     # Describe profile N:
  3243.     elsif (matches($args[0], "des|cribe")) {
  3244.         my $num =     $args[1] =~ /^\d+$/ ? $args[1] :
  3245.             do {
  3246.                 my $n = find_index($args[1], 1, @profiles);
  3247.                 if ($n < 1) {
  3248.                 $o->warn("No such profile '$args[1]'.\n");
  3249.                 return;
  3250.                 }
  3251.                 $n;
  3252.             } if defined $args[1];
  3253.         my $prof;
  3254.         if (defined $num and $num =~ /^\d+$/) {
  3255.         if (bounded(1, $num, scalar @profiles)) {
  3256.             $prof = $profiles[$num - 1];
  3257.         }
  3258.         else {
  3259.             $o->warn("No such profile number '$num'.\n");
  3260.             return;
  3261.         }
  3262.         }
  3263.         elsif (defined $num) {
  3264.         $o->warn("Argument to '$args[0]' must be numeric; see 'help profile'.\n");
  3265.         return;
  3266.         }
  3267.         else {
  3268.         $prof = $profile;
  3269.         }
  3270.  
  3271.         my $res = PPM::UI::profile_info($prof);
  3272.         $o->warn($res->msg) and return unless $res->ok;
  3273.         my @res = $res->result_l;
  3274.         {
  3275.         my ($pkg, $version, $target);
  3276.         my $picture = <<'END';
  3277. [[[[[[[[[[[[[[[[[[[    [[[[[[[[[[[    [[[[[[[[[[[[[[[[[[[[[[
  3278. END
  3279.         ($pkg, $version, $target) = qw(PACKAGE VERSION TARGET);
  3280.         my $text = '';
  3281.         $text .= form($picture, $pkg, $version, $target)
  3282.           if @res;
  3283.         for my $entity (@res) {
  3284.             ($pkg, $version, $target) = @$entity;
  3285.             $version = "[$version]";
  3286.             $text .= form($picture, $pkg, $version, $target);
  3287.         }
  3288.         if (@res) {
  3289.             $o->inform("Describing Profile '$prof':\n");
  3290.         }
  3291.         else {
  3292.             $o->inform("Profile '$prof' is empty.\n");
  3293.         }
  3294.         $o->page($text);
  3295.         }
  3296.         return 1;
  3297.     }
  3298.  
  3299.     # Add a profile "name":
  3300.     elsif (matches($args[0], "a|dd")) {
  3301.         my $name = $args[1];
  3302.         if ($name) {
  3303.         # Note: do some heavy-duty error-checking; XXX
  3304.         PPM::UI::profile_add($name);
  3305.         PPM::UI::profile_save($name)
  3306.           if $o->conf('profile-track');
  3307.         PPM::UI::profile_set($name)
  3308.           unless $which >= 0;
  3309.         @profiles = PPM::UI::profile_list()->result_l;
  3310.         }
  3311.         else {
  3312.         $o->warn("Invalid use of 'add' command; see 'help profile'.\n");
  3313.         return;
  3314.         }
  3315.     }
  3316.  
  3317.     # Remove profile N:
  3318.     elsif (matches($args[0], "del|ete")) {
  3319.         my $num =    $args[1] =~ /^\d+$/ ? $args[1] :
  3320.             do {
  3321.                 my $n = find_index($args[1], 1, @profiles);
  3322.                 if ($n < 1) {
  3323.                 $o->inform("No such profile '$args[1]'.\n");
  3324.                 return;
  3325.                 }
  3326.                 $n;
  3327.             } if defined $args[1];
  3328.         if (defined $num and $num =~ /^\d+$/) {
  3329.         my $dead_profile = $profiles[$num-1];
  3330.         if (bounded(1, $num, scalar @profiles)) {
  3331.             PPM::UI::profile_del($dead_profile);
  3332.             @profiles = dictsort PPM::UI::profile_list()->result_l;
  3333.             if (@profiles and $dead_profile eq $profile) {
  3334.             $profile = $profiles[0];
  3335.             PPM::UI::profile_set($profile);
  3336.             }
  3337.             elsif (not @profiles) {
  3338.             $o->conf('profile-track', 0);
  3339.             PPM::UI::profile_set('');
  3340.             }
  3341.         }
  3342.         else {
  3343.             $o->warn("No such profile '$num'.\n");
  3344.             return;
  3345.         }
  3346.         }
  3347.         elsif (defined $num) {
  3348.         $o->warn(<<END);
  3349. Argument to '$args[0]' must be numeric; see 'help profile'.
  3350. END
  3351.         return;
  3352.         }
  3353.         else {
  3354.         $o->warn(<<END);
  3355. Invalid use of '$args[0]' command; see 'help profile'.
  3356. END
  3357.         return;
  3358. }
  3359.     }
  3360.  
  3361.     # Save current profile:
  3362.     elsif (matches($args[0], "s|ave")) {
  3363.         unless (@profiles) {
  3364.         $o->warn(<<END);
  3365. No profiles on the server. Use 'profile add' to add a profile.
  3366. END
  3367.         return;
  3368.         }
  3369.         unless ($which >= 0) {
  3370.         $o->warn(<<END);
  3371. No profile selected. Use 'profile <number>' to select a profile.
  3372. END
  3373.         return;
  3374.         }
  3375.         my $ok = PPM::UI::profile_save($profile);
  3376.         if ($ok->ok) {
  3377.         $o->inform("Profile '$profile' saved.\n");
  3378.         }
  3379.         else {
  3380.         $o->warn($ok->msg);
  3381.         return;
  3382.         }
  3383.         return 1;
  3384.     }
  3385.  
  3386.     # Rename profile:
  3387.     elsif (matches($args[0], "ren|ame")) {
  3388.         unless (@profiles) {
  3389.         $o->warn(<<END);
  3390. No profiles on the server. Use 'profile add' to add a profile.
  3391. END
  3392.         return;
  3393.         }
  3394.  
  3395.         # Determine the old name:
  3396.         my $num =    $args[1] =~ /^\d+$/ ? $args[1] :
  3397.             do {
  3398.                 my $n = find_index($args[1], 1, @profiles);
  3399.                 if ($n < 1) {
  3400.                 $o->warn("No such profile '$args[1]'.\n");
  3401.                 return;
  3402.                 };
  3403.                 $n;
  3404.             } if defined $args[1];
  3405.         my $oldprof;
  3406.         if (defined $num and $num =~ /^\d+$/) {
  3407.         if (bounded(1, $num, scalar @profiles)) {
  3408.             $oldprof = $profiles[$num - 1];
  3409.         }
  3410.         else {
  3411.             $o->warn("No such profile number '$num'.\n");
  3412.             return;
  3413.         }
  3414.         }
  3415.         elsif (defined $num) {
  3416.         $o->warn("Argument to '$args[0]' must be numeric; see 'help profile'.\n");
  3417.         return;
  3418.         }
  3419.         else {
  3420.         $o->warn("profile: invalid use of '$args[0]' command: see 'help profile'.\n");
  3421.         return;
  3422.         }
  3423.  
  3424.         # Validate the new name:
  3425.         my $newprof = $args[2];
  3426.         unless (defined $newprof and length($newprof)) {
  3427.         $newprof = '' unless defined $newprof;
  3428.         $o->warn(<<END);
  3429. Profile names must be non-empty: '$newprof' is not a valid name.
  3430. END
  3431.         return;
  3432.         }
  3433.  
  3434.         # Actually do it:
  3435.         my $ok = PPM::UI::profile_rename($oldprof, $newprof);
  3436.         unless ($ok->is_success) {
  3437.         $o->warn($ok->msg);
  3438.         return unless $ok->ok;
  3439.         }
  3440.         if ($profile eq $oldprof) {
  3441.         $profile = $newprof;
  3442.         PPM::UI::profile_set($profile);
  3443.         }
  3444.         @profiles = dictsort PPM::UI::profile_list()->result_l;
  3445.     }
  3446.  
  3447.     # Restore current profile:
  3448.     elsif (matches($args[0], "res|tore")) {
  3449.         unless (@profiles) {
  3450.         $o->warn(<<END);
  3451. No profiles on this server. Use 'profile add' to add a profile.
  3452. END
  3453.         return;
  3454.         }
  3455.         unless ($which >= 0) {
  3456.         $o->warn(<<END);
  3457. No profile selected. Use 'profile <number>' to select a profile.
  3458. END
  3459.         return;
  3460.         }
  3461.         my ($clean_packages, $dry) = (0, 0);
  3462.         my ($force, $follow) = (1, 0);
  3463.         {
  3464.         local @ARGV = @args;
  3465.         GetOptions('clean!' => \$clean_packages,
  3466.                'force!' => \$force,
  3467.                'follow!' => \$follow,
  3468.                'dryrun' => \$dry,
  3469.               );
  3470.         @args = @ARGV;
  3471.         }
  3472.         my $cb_inst = $dry ? \&dr_install : \&cb_install;
  3473.         my $cb_rm   = $dry ? \&dr_remove  : \&cb_remove ;
  3474.         my $ok = PPM::UI::profile_restore($profile, sub {$o->$cb_inst(@_)},
  3475.                           sub {$o->$cb_rm(@_)}, $force, $follow,
  3476.                           $dry, $clean_packages);
  3477.         if ($ok->ok) {
  3478.         $o->cache_clear('query');
  3479.         $o->inform("Profile '$profile' restored.\n");
  3480.         }
  3481.         else {
  3482.         $o->warn($ok->msg);
  3483.         return;
  3484.         }
  3485.         return 1;
  3486.     }
  3487.  
  3488.     # Unrecognized subcommand:
  3489.     else {
  3490.         $o->warn("No such profile command '$args[0]'; see 'help profile'.\n");
  3491.         return;
  3492.     }
  3493.     }
  3494.     if (@profiles) {
  3495.     @profiles = dictsort @profiles;
  3496.     my $i = 0;
  3497.     $o->inform("Profiles:\n");
  3498.     my $profile = PPM::UI::profile_get()->result;
  3499.     for (@profiles) {
  3500.         $o->informf("%s%2d", $profile eq $profiles[$i] ? "*" : " ", $i + 1);
  3501.         $o->inform(". $_\n");
  3502.         $i++;
  3503.     }
  3504.     }
  3505.     elsif (defined $args[0] and matches($args[0], "del|ete")) {
  3506.     # assume that we just deleted the last profile
  3507.     $o->warn(<<END);
  3508. Profile deleted; no remaining profiles on the server.
  3509. END
  3510.     }
  3511.     else {
  3512.     $o->warn(<<END);
  3513. No profiles. Use 'profile add' to add a profile.
  3514. END
  3515.     }
  3516.     1;
  3517. }
  3518.  
  3519. #============================================================================
  3520. # Help-only topics:
  3521. #============================================================================
  3522. sub smry_prompt { "how to interpret the PPM prompt" }
  3523. sub help_prompt { <<'END' }
  3524. prompt -- information about the PPM3 prompt
  3525.   Description
  3526.     The PPM prompt can tell you six things:
  3527.  
  3528.     1)  The current repository;
  3529.  
  3530.     2)  The current target;
  3531.  
  3532.     3)  The last search you made on the current repository;
  3533.  
  3534.     4)  The last query you made on the current target;
  3535.  
  3536.     5)  The last package you described from this repository; and,
  3537.  
  3538.     6)  The last package you described from this target.
  3539.  
  3540.     To enable the prompt to tell you this information, you must set
  3541.     'prompt-context' to '1'. The following examples all assume this setting.
  3542.  
  3543.   Examples
  3544.     1   Repository and Target:
  3545.  
  3546.         Set 'prompt-context' The prompt will resemble:
  3547.  
  3548.             ppm:1:1> 
  3549.  
  3550.         In this case, the first '1' means that the first repository is
  3551.         selected. The second '1' means the first target is selected. You can
  3552.         prove this by adding another repository and switching to it:
  3553.  
  3554.             ppm:1:1> rep add TEMP http://my/repository
  3555.             Repositories:
  3556.               1. ActiveState Package Repository
  3557.             * 2. TEMP
  3558.             ppm:1:1> rep 2
  3559.             Repositories:
  3560.               1. ActiveState Package Repository
  3561.             * 2. TEMP
  3562.             ppm:2:1> 
  3563.  
  3564.         The same is true for targets. If you have multiple versions of Perl
  3565.         installed, when you swtich to a different target the second number
  3566.         reflects the change.
  3567.  
  3568.         If you delete all the repositories, the repository number changes to
  3569.         '?'. The same goes for targets. If either item is indicated by a
  3570.         question mark, you must configure a repository or target before
  3571.         proceeding.
  3572.  
  3573.     2   Search and Query:
  3574.  
  3575.         PPM stores searches and search results from in the current session.
  3576.         The prompt displays the search number:
  3577.  
  3578.             ppm:1:1> search Text
  3579.             [results displayed here]
  3580.             ppm:1:1:s1> 
  3581.  
  3582.         The 's1' indicates that the last search you performed can be viewed
  3583.         again by entering 'search 1'. Type 'search' with no arguments to
  3584.         view the list of cached searches:
  3585.  
  3586.             ppm:1:1:s1> search
  3587.             Search Result Sets:
  3588.             * 1. Text
  3589.  
  3590.         If you then enter 'search 1', you will see the same results as when
  3591.         you typed 'search Text' earlier. If you search for something else
  3592.         ('search Parse') then the number will change to 's2':
  3593.  
  3594.             ppm:1:1:s1> search Parse
  3595.             [results displayed here]
  3596.             ppm:1:1:s2>
  3597.  
  3598.         The same indicators apply to the query command. When you run a
  3599.         query, a numerical indicator displays the current query:
  3600.  
  3601.             ppm:1:1:s1> query PPM
  3602.             [results displayed here]
  3603.             ppm:1:1:s1:q1> 
  3604.  
  3605.         You can view the past queries with 'query', and view results by
  3606.         querying a particular number.
  3607.  
  3608.     3   Describe and Properties:
  3609.  
  3610.         When you use the describe command with the numerical switch (to view
  3611.         package information based on the package number in the last search
  3612.         or query), PPM sets that index to the current index. If you use the
  3613.         desribe command with the name switch, and the name is found within
  3614.         the current result, the index is set to the current one. If no
  3615.         package is found, PPM creates a new search or query on-the-fly, and
  3616.         sets it as the current search or query.
  3617.  
  3618.         For example:
  3619.  
  3620.             ppm:1:1> search Text
  3621.             1. Convert-Context  [0.501]     an Attributed Text data type
  3622.             2. gettext          [1.01]      message handling functions
  3623.             3. HTML-FromText    [1.005]     mark up text as HTML
  3624.             4. HTML-Subtext     [1.03]      Perform text substitutions on an HTML
  3625.                                             template
  3626.             5. Locale-Maketext  [0.18]      framework for software localization
  3627.             ppm:1:1:s1>
  3628.  
  3629.             ppm:1:1:s1> describe 1
  3630.             ====================
  3631.             Package 1:
  3632.                 Name: Convert-Context
  3633.              Version: 0.501
  3634.               Author: Martin Schwartz (martin@nacho.de)
  3635.             Abstract: an Attributed Text data type
  3636.             Implementations:
  3637.                    1. i686-linux-thread-multi
  3638.                    2. MSWin32-x86-multi-thread
  3639.                    3. sun4-solaris-thread-multi
  3640.             ====================
  3641.             ppm:1:1:s1:sp1> 
  3642.  
  3643.         The last prompt has an extra 'sp1'. That stands for 'search package
  3644.         1', and it means that PPM considers 'Convert-Context' to be the
  3645.         default package. If you now type 'describe' or 'install' with no
  3646.         arguments, PPM will apply your command to this package.
  3647.  
  3648.         If you go back to where you had no default package selected:
  3649.  
  3650.             ppm:1:1> search Text
  3651.             1. Convert-Context  [0.501]     an Attributed Text data type
  3652.             2. gettext          [1.01]      message handling functions
  3653.             3. HTML-FromText    [1.005]     mark up text as HTML
  3654.             4. HTML-Subtext     [1.03]      Perform text substitutions on an HTML
  3655.                                             template
  3656.             5. Locale-Maketext  [0.18]      framework for software localization
  3657.             ppm:1:1:s1>
  3658.  
  3659.         ...and you describe 'Locale-Maketext', you will see this:
  3660.  
  3661.             ppm:1:1:s1> describe Locale-Maketext
  3662.             ====================
  3663.                 Name: Locale-Maketext
  3664.              Version: 0.18
  3665.               Author: Sean M. Burke (sburke@cpan.org)
  3666.             Abstract: framework for software localization
  3667.             Prerequisites:
  3668.                    1. I18N-LangTags 0.13
  3669.             Implementations:
  3670.                    1. i686-linux-thread-multi
  3671.                    2. MSWin32-x86-multi-thread
  3672.                    3. sun4-solaris-thread-multi
  3673.             ====================
  3674.             ppm:1:1:s1:sp5>
  3675.  
  3676.         Notice that the correct package got selected, even though you
  3677.         specified it by name.
  3678.  
  3679.     This behaviour also applies to the query and properties commands.
  3680.  
  3681.   See Also
  3682.     describe, properties, query, search
  3683. END
  3684.  
  3685. #sub run_quickstart  { $_[0]->run_help('quickstart') }
  3686. sub smry_quickstart { "a crash course in using PPM" }
  3687. sub help_quickstart { <<'END' }
  3688. quickstart -- a beginners' guide to PPM3
  3689.   Description
  3690.     PPM (Programmer's Package Manager) is a utility for managing software
  3691.     "packages". A package is a modular extension for a language or a
  3692.     software program. Packages reside in repositories. PPM can use three
  3693.     types of repositories:
  3694.  
  3695.      1) A directory on a CD-ROM or hard drive in your computer
  3696.      2) A website
  3697.      3) A remote Repository Server (such as ASPN)
  3698.  
  3699.     Common Commands:
  3700.  
  3701.     To view PPM help:
  3702.  
  3703.       help
  3704.       help <command>
  3705.  
  3706.     To view the name of the current repository:
  3707.  
  3708.       repository
  3709.  
  3710.     To search the current repository:
  3711.  
  3712.       search <keywords>
  3713.  
  3714.     To install a package:
  3715.  
  3716.       install <package_name>
  3717.  
  3718.     Most commands can be truncated; as long as the command is unambiguous,
  3719.     PPM will recognize it. For example, 'repository add foo' can be entered
  3720.     as 'rep add foo'.
  3721.  
  3722.     PPM features user profiles, which store information about installed
  3723.     packages. Profiles are stored as part of your ASPN account; thus, you
  3724.     can easily maintain package profiles for different languages, or
  3725.     configure one machine with your favorite packages, and then copy that
  3726.     installation to another machine by accessing your ASPN profile.
  3727.  
  3728.     For more information, type 'help profile' at the PPM prompt.
  3729. END
  3730.  
  3731. sub smry_ppm_migration { "guide for those familiar with PPM" }
  3732. sub help_ppm_migration { <<'END' }
  3733. ppm migration -- PPM Migration Guide
  3734.   Description
  3735.     Those familiar with PPM version 2 should appreciate the extended
  3736.     functionality of PPM version 3, including the command-line history,
  3737.     autocomplete and profiles. Some PPM version 2 commands are different in
  3738.     PPM version 3. Examples of command changes include:
  3739.  
  3740.     1   Adding a repository
  3741.  
  3742.         PPM2:
  3743.  
  3744.           set repository my_repository http://my/repository
  3745.  
  3746.         PPM3:
  3747.  
  3748.           repository add my_repository http://my/repository
  3749.  
  3750.     2   Removing a repository
  3751.  
  3752.         PPM2:
  3753.  
  3754.           set repository --remove my_repository
  3755.  
  3756.         PPM3:
  3757.  
  3758.           repository del my_repository
  3759.  
  3760.     3   Setting the temporary directory
  3761.  
  3762.         PPM2:
  3763.  
  3764.           set build DIRECTORY
  3765.  
  3766.         PPM3
  3767.  
  3768.           set tempdir DIRECTORY
  3769.  
  3770.     4   Setting frequency of download updates
  3771.  
  3772.         PPM2:
  3773.  
  3774.           set downloadstatus NUMBER
  3775.  
  3776.         PPM3:
  3777.  
  3778.           set download-chunksize NUMBER
  3779.  
  3780.     5   Changing the installation root directory:
  3781.  
  3782.         PPM2:
  3783.  
  3784.           set root DIRECTORY
  3785.  
  3786.         PPM3:
  3787.  
  3788.           target set root DIRECTORY
  3789.  
  3790.     6   Listing all installed packages:
  3791.  
  3792.         PPM2:
  3793.  
  3794.           query
  3795.  
  3796.         PPM3:
  3797.  
  3798.           query *
  3799.  
  3800.     7   Listing all packages on server:
  3801.  
  3802.         PPM2:
  3803.  
  3804.           search
  3805.  
  3806.         PPM3:
  3807.  
  3808.           search *
  3809.  
  3810.     8   Enabling HTML documentation generation:
  3811.  
  3812.         PPM2:
  3813.  
  3814.           set rebuildhtml 1
  3815.  
  3816.         PPM3:
  3817.  
  3818.           set rebuild-html 1
  3819. END
  3820.  
  3821. sub smry_unicode { "notes about unicode author names" }
  3822. sub help_unicode { <<'END' }
  3823. unicode -- Notes About Unicode Author Names
  3824.   Description
  3825.     CPAN author names are defined to be in Unicode. Unicode is an
  3826.     international standard ISO 10646, defining the *Universal Character Set
  3827.     (UCS)*. UCS contains all characters of all other character set
  3828.     standards. For more information about Unicode, see
  3829.     http://www.unicode.org/.
  3830.  
  3831.     The CPAN authors website is located at your local CPAN mirror under
  3832.     /authors/00whois.html. For example, you can view it at
  3833.     http://www.cpan.org/authors/00whois.html. This page can be rendered by
  3834.     Mozilla 0.9.8 and Internet Explorer 5.0, but you may have to install
  3835.     extra language packs to view all the author names.
  3836.  
  3837.     By default, PPM3 renders all characters as Latin1 when it prints them to
  3838.     your console. Characters outside the Latin1 range (0-255) are not
  3839.     printed at all.
  3840.  
  3841.     If your console can render UTF-8 characters, you can tell PPM3 not to
  3842.     recode characters by using one of the following environment variables:
  3843.  
  3844.     *   LC_ALL
  3845.  
  3846.     *   LC_CTYPE
  3847.  
  3848.     *   LANG
  3849.  
  3850.     *   PPM_LANG
  3851.  
  3852.     PPM3 requires one of these environment variables to contain the string
  3853.     'UTF-8'. For example, the following setting make PPM3 print
  3854.     beautifully-formatted authors in RedHat Linux 7.2 (assumes you're using
  3855.     a Bourne shell):
  3856.  
  3857.       $ PPM_LANG='en_US.UTF-8' xterm -u8 -e ppm3
  3858.  
  3859.     Linux and Solaris users should refer to xterm for more information about
  3860.     setting up xterm to display UTF-8 characters.
  3861. END
  3862.  
  3863. #============================================================================
  3864. # Utility Functions
  3865. #============================================================================
  3866. sub sort_fields { qw(name title author abstract version repository) }
  3867. sub sort_pkgs {
  3868.     my $o = shift;
  3869.     my $field = lc shift;
  3870.     my @pkgs = @_;
  3871.     my $targ = $o->conf('target');
  3872.     my $filt = sub { $_[0]->getppd_obj($targ)->result->$field };
  3873.     if ($field eq 'name') {
  3874.     return dictsort $filt, @pkgs;
  3875.     }
  3876.     if ($field eq 'title') {
  3877.     return dictsort $filt, @pkgs;
  3878.     }
  3879.     if ($field eq 'author') {
  3880.     return dictsort $filt, @pkgs;
  3881.     }
  3882.     if ($field eq 'abstract') {
  3883.     return dictsort $filt, @pkgs;
  3884.     }
  3885.     if ($field eq 'repository') {
  3886.     return dictsort sub { $_[0]->repository->name }, @pkgs;
  3887.     }
  3888.     if ($field eq 'version') {
  3889.     return sort {
  3890.         my $pa = $a->getppd_obj($targ)->result;
  3891.         my $pb = $b->getppd_obj($targ)->result;
  3892.         $pb->uptodate($pa->version_osd) <=> $pa->uptodate($pb->version_osd)
  3893.     } @pkgs;
  3894.     }
  3895.     @pkgs;
  3896. }
  3897.  
  3898. sub find_index {
  3899.     my $entry = shift || '';
  3900.     my $index = shift;
  3901.     $index = 0 unless defined $index;
  3902.     for (my $i=0; $i<@_; $i++) {
  3903.     return $index + $i if $entry eq $_[$i];
  3904.     }
  3905.     return $index - 1;
  3906. }
  3907.  
  3908. sub bounded {
  3909.     my $lb = shift;
  3910.     my $d = shift;
  3911.     my $ub = shift;
  3912.     return ($d >= $lb and $d <= $ub);
  3913. }
  3914.  
  3915. sub dictsort(@) {
  3916.     my $o = shift if eval { $_[0]->isa("PPMShell") };
  3917.     my $filt = ref($_[0]) eq 'CODE' ? shift @_ : undef;
  3918.     return map { $_->[0] }
  3919.        sort { lc $a->[1] cmp lc $b->[1] }
  3920.        map { [ $_, $filt ? $filt->($_) : $_ ] } @_;
  3921. }
  3922.  
  3923. sub path_under {
  3924.     my $path = shift;
  3925.     my $cmp  = shift;
  3926.     if ($^O eq 'MSWin32') {
  3927.     $path =~ s#\\#/#g;
  3928.     $cmp  =~ s#\\#/#g;
  3929.     return $path =~ /^\Q$cmp\E/i;
  3930.     }
  3931.     else {
  3932.     return $path =~ /^\Q$cmp\E/;
  3933.     }
  3934. }
  3935.  
  3936. sub prompt_str {
  3937.     my $o = shift;
  3938.  
  3939.     # Hack: set the pager here, instead of in settings_setkey()
  3940.     $o->{API}{pager} = $o->conf('pager');
  3941.  
  3942.     my @search_results = $o->cache_sets('search');
  3943.     my $search_result_current = $o->cache_set_current('search');
  3944.     my $search_result_index = $o->cache_set_index('search');
  3945.     my @query_results = $o->cache_sets('query');
  3946.     my $query_result_current = $o->cache_set_current('query');
  3947.     my $query_result_index = $o->cache_set_index('query');
  3948.  
  3949.     # Make sure a profile is selected if they turned tracking on.
  3950.     my $profile_track = $o->conf('profile-track');
  3951.     my $profile       = PPM::UI::profile_get()->result;
  3952.     $o->setup_profile()
  3953.     if $profile_track and not $profile and $o->mode eq 'SHELL';
  3954.  
  3955.     my @targs = PPM::UI::target_list()->result_l;
  3956.     if (@targs and not find_index($o->conf('target'), 1, @targs)) {
  3957.     $o->conf('target', $targs[0]);
  3958.     }
  3959.  
  3960.     if ($o->conf('prompt-context')) {
  3961.     my ($targ, $rep, $s, $sp, $q, $qp);
  3962.  
  3963.     if ($o->conf('prompt-verbose')) {
  3964.         my $sz = $o->conf('prompt-slotsize');
  3965.         $targ = substr($o->conf('target'), 0, $sz);
  3966.         $rep  = substr($o->conf('repository'), 0, $sz);
  3967.  
  3968.         my $sq_tmp = $o->cache_set('search', undef, 'query');
  3969.         my $ss_tmp = $o->cache_set('search');
  3970.         my $sp_tmp = $o->cache_entry('search');
  3971.         $s = (defined $sq_tmp)
  3972.           ? ":" . substr($sq_tmp, 0, $sz)
  3973.           : "";
  3974.         $sp = ($s and defined $sp_tmp and
  3975.            bounded(0, $search_result_index, $#$ss_tmp))
  3976.           ? ":" . substr($sp_tmp->name, 0, $sz)
  3977.           : "";
  3978.  
  3979.         my $qq_tmp = $o->cache_set('query', undef, 'query');
  3980.         my $qs_tmp = $o->cache_set('query');
  3981.         my $qp_tmp = $o->cache_entry('query');
  3982.         $q = (defined $qq_tmp)
  3983.           ? ":" . substr($qq_tmp, 0, $sz)
  3984.           : "";
  3985.         $qp = ($q and defined $qp_tmp and
  3986.            bounded(0, $query_result_index, $#$qs_tmp))
  3987.           ? ":" . substr($qp_tmp->name, 0, $sz)
  3988.           : "";
  3989.     }
  3990.     else {
  3991.         # Target and Repository:
  3992.         $targ = find_index($o->conf('target'), 1, @targs);
  3993.         $targ = '?' if $targ == 0;
  3994.     
  3995.         # Search number & package:
  3996.         $s = @search_results ? ":s".($search_result_current + 1) : "";
  3997.         my $sp_tmp = $o->cache_set('search');
  3998.         $sp = ($s and defined $sp_tmp and 
  3999.            bounded(0, $search_result_index, $#$sp_tmp))
  4000.           ? ":sp".($search_result_index + 1)
  4001.           : "";
  4002.     
  4003.         # Query number & package:
  4004.         $q = @query_results ? ":q".($query_result_current + 1) : "";
  4005.         my $qp_tmp = $o->cache_set('query');
  4006.         $qp = ($q and defined $qp_tmp and
  4007.            bounded(0, $query_result_index, $#$qp_tmp))
  4008.           ? ":qp".($query_result_index + 1)
  4009.           : "";
  4010.     }
  4011.     return "ppm:$targ$s$sp$q$qp> ";
  4012.     }
  4013.     else {
  4014.     return "ppm> ";
  4015.     }
  4016. }
  4017.  
  4018. {
  4019.     # Weights for particular fields: these are stored in percentage of the
  4020.     # screen width, based on the number of columns they use on an 80 column
  4021.     # terminal. They also have a minimum and maximum.
  4022.     use constant MIN    => 0;
  4023.     use constant MAX    => 1;
  4024.     my %weight = (
  4025.     name     => [12, 20],
  4026.     title    => [12, 20],
  4027.     abstract => [12, 20],
  4028.     author   => [12, 20],
  4029.     repository => [12, 20],
  4030.     version  => [ 4,  9],
  4031.     );
  4032.     my %meth = (
  4033.     name     => 'name',
  4034.     title    => 'title',
  4035.     version  => 'version',
  4036.     abstract => 'abstract',
  4037.     author   => 'author',
  4038.     repository => sub {
  4039.         my $o = shift;
  4040.         my $rep = $o->repository or return "Installed";
  4041.         my $name = $rep->name;
  4042.         my $id   = $o->id || $name;
  4043.         my $loc  = $rep->location;
  4044.         "$name [$loc]"
  4045.     },
  4046.     );
  4047.     # These are Text::Autoformat justification marks. They're actually used to
  4048.     # build a printf() format string, since it's so much more efficient for a
  4049.     # non-line-wrapping case.
  4050.     my %just = (
  4051.     name     => '<',
  4052.     title    => '<',
  4053.     abstract => '<',
  4054.     author   => '<',
  4055.     repository => '<',
  4056.     version  => '>',
  4057.     );
  4058.     my %plus = (
  4059.     name     => '0',
  4060.     title    => '0',
  4061.     abstract => '0',
  4062.     author   => '0',
  4063.     repository => '0',
  4064.     version  => '2',
  4065.     );
  4066.     my %filt = (
  4067.     version => q{"[$_]"},
  4068.     );
  4069.     sub picture_optimized {
  4070.     my $o = shift;
  4071.     my @items = @{shift(@_)};
  4072.     unless ($o->conf('fields')) {
  4073.         my $m = $o->setmode('SILENT');
  4074.         $o->conf('fields', '', 1);
  4075.         $o->setmode($m);
  4076.     }
  4077.     my @fields = split ' ', $o->conf('fields');
  4078.     $_ = lc $_ for @fields;
  4079.     my (%max_width, %width);
  4080.     my $cols = $o->termsize->{cols};
  4081.     for my $f (@fields) {
  4082.         my $meth = $meth{$f};
  4083.         $max_width{$f} = max { length($_->$meth) } @items;
  4084.         $max_width{$f} += $plus{$f};
  4085.         $width{$f} = $max_width{$f} / 80 * $cols;
  4086.         my $max_f  = $weight{$f}[MAX] / 80 * $cols;
  4087.         my $min_f  = $weight{$f}[MIN];
  4088.         my $gw     = $width{$f};
  4089.         $width{$f} = (
  4090.         $width{$f} > $max_width{$f} ? $max_width{$f} :
  4091.         $width{$f} > $max_f         ? $max_f         :
  4092.         $width{$f} < $min_f         ? $min_f         : $width{$f}
  4093.         );
  4094.     }
  4095.     my $right = $fields[-1];
  4096.     my $index_sz = length( scalar(@items) ) + 3; # index spaces
  4097.     my $space_sz = @fields + 1; # separator spaces
  4098.     my $room = $cols - $index_sz - $space_sz;
  4099.     $width{$right} = $room - sum { $width{$_} } @fields[0 .. $#fields-1];
  4100.     while ($width{$right} > $max_width{$right}) {
  4101.         my $smallest;
  4102.         my $n;
  4103.         for my $k (@fields[0 .. $#fields-1]) {
  4104.         my $max = $max_width{$k};
  4105.         my $sz  = $width{$k};
  4106.         $smallest = $k, $n = $max - $sz if $max - $sz > $n;
  4107.         }
  4108.         $width{$right}--;
  4109.         $width{$smallest}++;
  4110.     }
  4111.     while ($width{$right} < $weight{$right}[MIN]) {
  4112.         my $biggest;
  4113.         my $n;
  4114.         for my $k (@fields[0 .. $#fields-1]) {
  4115.         my $max = $max_width{$k};
  4116.         my $sz  = $width{$k};
  4117.         $biggest = $k, $n = $max - $sz if $max - $sz < $n;
  4118.         }
  4119.         $width{$right}++;
  4120.         $width{$biggest}--;
  4121.     }
  4122.     my $picture;
  4123.     $picture = "\%${index_sz}s "; # printf picture
  4124.     $picture .= join ' ', map {
  4125.         my $w = $width{$_};
  4126.         my $c = $just{$_};
  4127.         my $pad = $c eq '>' ? '' : '-';
  4128.         "\%${pad}${w}s" # printf picture
  4129.     } @fields;
  4130.     ($picture, \@fields, [@width{@fields}]);
  4131.     }
  4132.  
  4133.     sub print_formatted {
  4134.     my $o = shift;
  4135.     my $targ = $o->conf('target');
  4136.     my @items = map { $_->getppd_obj($targ)->result } @{shift(@_)};
  4137.     my $selected = shift;
  4138.     my $format;
  4139.  
  4140.     # Generate a picture and a list of fields for Text::Autoformat:
  4141.     my (@fields, %width);
  4142.     my ($picture, $f, $w) = $o->picture_optimized(\@items);
  4143.     $picture .= "\n";
  4144.     @fields = @$f;
  4145.     @width{@fields} = @$w;
  4146.  
  4147.     # The line-breaking sub: use '~' as hyphenation signal
  4148.     my $wrap = sub {
  4149.         my ($str, $maxlen, $width) = @_;
  4150.         my $field = substr($str, 0, $maxlen - 1) . '~';
  4151.         my $left  = substr($str, $maxlen - 1);
  4152.         ($field, $left);
  4153.     };
  4154.  
  4155.     my $lines = 0;
  4156.     my $i = 1;
  4157.     my @text;
  4158.     my %seen;
  4159.     for my $pkg (@items) {
  4160.         my $star = (defined $selected and $selected == $i - 1) ? "*" : " ";
  4161.         my $num  = "$star $i.";
  4162.         my @vals = (
  4163.         map {
  4164.             my $field  = $_;
  4165.             my $method = $meth{$field};
  4166.             local $_   = $pkg->$method;
  4167.             my $val = defined $filt{$field} ? eval $filt{$field} : $_;
  4168.             ($val) = $wrap->($val, $width{$field})
  4169.                 if length $val > $width{$field};
  4170.             $val;
  4171.         }
  4172.         @fields
  4173.         );
  4174. #        my $key = join '', @vals;
  4175. #        if (exists $seen{$key}) {
  4176. #        my $index = $seen{$key};
  4177. #        substr($text[$index], 0, 1) = '+';
  4178. #        next;
  4179. #        }
  4180. #        $seen{$key} = $i - 1;
  4181.         (my $inc = sprintf $picture, $num, @vals) =~ s/[ ]+$//;
  4182.         push @text, $inc;
  4183.         $i++;
  4184.     }
  4185.  
  4186.     # And, page it.
  4187.     $o->page(join '', @text);
  4188.     }
  4189. }
  4190.  
  4191. sub tree_pkg {
  4192.     my $o = shift;
  4193.     my @rlist = $o->reps_on;
  4194.     my $tar = $o->conf('target');
  4195.     my $pkg = shift;
  4196.     my $ppd;
  4197.     if (eval { $pkg->isa('PPM::Package') }) {
  4198.     $ppd = $pkg->getppd_obj($tar);
  4199.     unless ($ppd->ok) {
  4200.         $o->warn($ppd->msg);
  4201.         return;
  4202.     }
  4203.     $ppd = $ppd->result;
  4204.     }
  4205.     else {
  4206.     my ($s, $i) = $o->cache_find('search', $pkg);
  4207.     if ($i >= 0) {
  4208.         $ppd = $o->cache_entry('search', $i, $s);
  4209.     } 
  4210.     else {
  4211.         my $ok = PPM::UI::describe(\@rlist, $tar, $pkg);
  4212.         unless ($ok->is_success) {
  4213.         $o->warn($ok->msg);
  4214.         return unless $ok->ok;
  4215.         }
  4216.         $ppd = $ok->result->getppd_obj($tar);
  4217.         unless ($ppd->ok) {
  4218.         $o->warn($ppd->msg);
  4219.         return;
  4220.         }
  4221.         $ppd = $ppd->result;
  4222.     }
  4223.     }
  4224.  
  4225.     my $pad = "\n";
  4226.     $o->inform($ppd->name, " ", $ppd->version);
  4227.     $o->Tree(\@rlist, $tar, $ppd->name, $pad, {});
  4228.     $o->inform($pad);
  4229. }
  4230.  
  4231. my ($VER, $HOR, $COR, $TEE, $SIZ) = ('|', '_', '\\', '|', ' ');
  4232.  
  4233. sub Tree {
  4234.     my $o = shift;
  4235.     my $reps = shift;
  4236.     my $tar = shift;
  4237.     my $pkg = shift;
  4238.     my $ind = shift;
  4239.     my $seen = shift;
  4240.     my $pad = $ind . "  " . $VER;
  4241.  
  4242.     my $ppd;
  4243.     if (exists $seen->{$pkg}) {
  4244.     $ppd = $seen->{$pkg};
  4245.     }
  4246.     else {
  4247.     my ($s, $i) = $o->cache_find('search', $pkg);
  4248.     if ($i >= 0) {
  4249.         $ppd = $o->cache_entry('search', $i, $s);
  4250.     }
  4251.     else {
  4252.         my $ok = PPM::UI::describe($reps, $tar, $pkg);
  4253.         unless ($ok->is_success) {
  4254.         $o->inform(" -- package not found; skipping tree");
  4255.         return 0 unless $ok->ok;
  4256.         }
  4257.         $ppd = $ok->result;
  4258.     }
  4259.     $ppd->make_complete($tar);
  4260.     $ppd = $ppd->getppd_obj($tar);
  4261.     unless ($ppd->ok) {
  4262.         $o->warn($ppd->msg);
  4263.         return;
  4264.     }
  4265.     $ppd = $ppd->result;
  4266.     $seen->{$pkg} = $ppd;
  4267.     }
  4268.  
  4269.     my @impls   = $ppd->implementations;
  4270.     return 0 unless @impls;
  4271.     my @prereqs = $impls[0]->prereqs;
  4272.     return 0 unless @prereqs;
  4273.     my $nums = scalar @prereqs;
  4274.  
  4275.     for (1..$nums) {
  4276.     my $doneblank = 0;
  4277.     my $pre = $prereqs[$_-1];
  4278.     my $txt = $pre->name . " " . $pre->version;
  4279.     if ($_ == $nums) {
  4280.         substr($pad, -1) = $COR;
  4281.         $o->inform($pad, "$HOR$HOR", $txt);
  4282.         substr($pad, -1) = ' ';
  4283.     }
  4284.     else {
  4285.         substr($pad, -1) = $TEE;
  4286.         $o->inform($pad, "$HOR$HOR", $txt);
  4287.         substr($pad, -1) = $VER;
  4288.     }
  4289.     if ($o->Tree($reps, $tar, $pre->name, $pad, $seen) != 0 and
  4290.         $doneblank == 0) {
  4291.         $o->inform($pad); ++$doneblank;
  4292.     }
  4293.     }
  4294.     return $nums;
  4295. }
  4296.  
  4297. sub describe_pkg {
  4298.     my $o = shift;
  4299.     my $pkg = shift;
  4300.     my ($extra_keys, $extra_vals) = (shift || [], shift || []);
  4301.     my $n; 
  4302.  
  4303.     # Get the PPM::PPD object out of the PPM::Package object.
  4304.     my $pkg_des = $pkg->describe($o->conf('target'));
  4305.     unless ($pkg_des->ok) {
  4306.     $o->warn($pkg_des->msg);
  4307.     return;
  4308.     }
  4309.     $pkg_des = $pkg_des->result;
  4310.  
  4311.     # Basic information:
  4312.     $n = $o->print_pairs(
  4313.     [qw(Name Version Author Title Abstract), @$extra_keys],
  4314.     [(map { $pkg_des->$_ } qw(name version author title abstract)),
  4315.      @$extra_vals],
  4316.     undef,    # separator
  4317.     undef,    # left
  4318.     undef,    # indent
  4319.     undef,    # length
  4320.     1,    # wrap (yes, please wrap)
  4321.     );
  4322.  
  4323.     # The repository:
  4324.     if (my $rep = $pkg_des->repository) {
  4325.     $o->print_pairs(
  4326.         ["Location"],
  4327.         [$rep->name],
  4328.         undef,    # separator
  4329.         undef,    # left
  4330.         undef,    # indent
  4331.         $n,        # length
  4332.         1,        # wrap
  4333.     );
  4334.     }
  4335.     
  4336.     # Prerequisites:
  4337.     my @impls = grep { $_->architecture } $pkg_des->implementations;
  4338.     my @prereqs = @impls ? $impls[0]->prereqs : ();
  4339.     $o->inform("Prerequisites:\n") if @prereqs;
  4340.     $o->print_pairs(
  4341.     [ 1 .. @prereqs ],
  4342.     [ map { $_->name . ' ' . $_->version} @prereqs ],
  4343.     '. ',    # separator
  4344.     undef,    # left
  4345.     undef,    # indent
  4346.     $n,    # length
  4347.     0,    # wrap (no, please don't wrap)
  4348.     );
  4349.     
  4350.     # Implementations:
  4351.     $o->inform("Available Platforms:\n") if @impls;
  4352.     my @impl_strings;
  4353.     for (@impls) {
  4354.     my $arch  = $_->architecture;
  4355.     my $os    = $_->os;
  4356.     my $osver = $_->osversion;
  4357.     my $str   = $arch;
  4358.     $osver    =~ s/\Q(any version)\E//g;
  4359.     if ($os and $osver) {
  4360.         $str .= ", $os $osver";
  4361.     }
  4362.     push @impl_strings, $str;
  4363.     }
  4364.     @impl_strings = dictsort @impl_strings;
  4365.     $o->print_pairs(
  4366.     [ 1 .. @impls ],
  4367.     [ @impl_strings ],
  4368.     '. ', undef, undef, $n
  4369.     );
  4370. }
  4371.  
  4372. sub remove_pkg {
  4373.     my $o = shift;
  4374.     my $package = shift;
  4375.     my $target = $o->conf('target');
  4376.     my $force = shift;
  4377.     my $quell_clear = shift;
  4378.     my $verbose = $o->conf('remove-verbose');
  4379.     my $ok = PPM::UI::remove($target, $package, $force, sub { $o->cb_remove(@_) }, $verbose);
  4380.     unless ($ok->is_success) {
  4381.     $o->warn($ok->msg);
  4382.     return 0 unless $ok->ok;
  4383.     }
  4384.     else {
  4385.     $o->warn_profile_change($ok);
  4386.     }
  4387.     $o->cache_clear('query') if ($ok->ok and not $quell_clear);
  4388.     1;
  4389. }
  4390.  
  4391. sub upgrade_pkg {
  4392.     push @_, 'upgrade';
  4393.     goto &install_pkg;
  4394. }
  4395. sub install_pkg {
  4396.     my $o = shift;
  4397.     my $pkg = shift;
  4398.     my $opts = shift;
  4399.     my $action = shift;
  4400.     my $quell_clear = shift;
  4401.     $action = 'install' unless defined $action;
  4402.  
  4403.     # Find the package:
  4404.     while (1) {
  4405.     # 1. Return if they specified a full filename or URL:
  4406.     last if PPM::UI::is_pkg($pkg);
  4407.  
  4408.     # 2. Check if whatever they specified returns 1 search result:
  4409.     my $search =
  4410.       PPM::UI::search([$o->reps_on], $o->conf('target'), $pkg, 
  4411.               $o->conf('case-sensitivity'));
  4412.     unless ($search->is_success) {
  4413.         $o->warn($search->msg);
  4414.         return unless $search->ok;
  4415.     }
  4416.     my @ret = $search->result_l;
  4417.     if (@ret > 1) {
  4418.         $o->warn(<<END);
  4419. Searching for '$pkg' returned multiple results. Using 'search' instead...
  4420. END
  4421.         $o->run_search($pkg);
  4422.         return;
  4423.     }
  4424.     elsif (not @ret) {
  4425.         $o->warn(<<END);
  4426. Searching for '$pkg' returned no results. Try a broader search first.
  4427. END
  4428.         return;
  4429.     }
  4430.     $pkg = $ret[0]->name;
  4431.     last;
  4432.     }
  4433.  
  4434.     my $cb = (
  4435.     $opts->{dryrun}
  4436.     ? $action eq 'install' ? \&dr_install : \&dr_upgrade
  4437.     : $action eq 'install' ? \&cb_install : \&cb_upgrade
  4438.     );
  4439.  
  4440.     # Now, do the install
  4441.     my $ok;
  4442.     my @rlist = $o->reps_on;
  4443.     my $targ = $o->conf('target');
  4444.  
  4445.     my $prop = PPM::UI::properties($targ, $pkg);
  4446.     if ($prop->ok) {
  4447.     my $name = ($prop->result_l)[0]->name;
  4448.     if (ref $pkg) {
  4449.         $pkg->name($name);
  4450.     }
  4451.     else {
  4452.         $pkg = $name;
  4453.     }
  4454.     }
  4455.  
  4456.     if ($action eq 'install') {
  4457.     $opts->{verbose} = $o->conf('install-verbose');
  4458.     my $pkgname = ref $pkg ? $pkg->name : $pkg;
  4459.     if ($prop->ok) {
  4460.         $o->inform("Note: Package '$pkgname' is already installed.\n");
  4461.         return unless $opts->{force};
  4462.     }
  4463.     $ok = PPM::UI::install(\@rlist, $targ, $pkg, $opts, sub {$o->$cb(@_)});
  4464.     }
  4465.     else {
  4466.     $opts->{verbose} = $o->conf('upgrade-verbose');
  4467.     $ok = PPM::UI::upgrade(\@rlist, $targ, $pkg, $opts, sub {$o->$cb(@_)});
  4468.     }
  4469.  
  4470.     unless ($ok->is_success) {
  4471.     $o->warn($ok->msg);
  4472.     return unless $ok->ok;
  4473.     }
  4474.     else {
  4475.     $o->warn_profile_change($ok);
  4476.     $o->cache_clear('query') unless $quell_clear;
  4477.     }
  4478.     1;
  4479. }
  4480.  
  4481. # The dry run callback; just prints out package name and version:
  4482. sub dr_install {
  4483.     my $o = shift;
  4484.     my $pkg = shift;
  4485.     my $version = shift;
  4486.     my $target_name = shift;
  4487.     $o->inform(<<END);
  4488. Dry run install '$pkg' version $version in $target_name.
  4489. END
  4490. }
  4491.  
  4492. sub dr_upgrade {
  4493.     my $o = shift;
  4494.     my $pkg = shift;
  4495.     my $version = shift;
  4496.     my $target_name = shift;
  4497.     $o->inform(<<END);
  4498. Dry run upgrade '$pkg' version $version in $target_name.
  4499. END
  4500. }
  4501.  
  4502. sub dr_remove {
  4503.     my $o = shift;
  4504.     my $pkg = shift;
  4505.     my $version = shift;
  4506.     my $target_name = shift;
  4507.     $o->inform(<<END);
  4508. Dry run remove '$pkg' version $version from $target_name.
  4509. END
  4510. }
  4511.  
  4512. sub cb_remove {
  4513.     my $o = shift;
  4514.     my $pkg = shift;
  4515.     my $version = shift;
  4516.     my $target_name = shift;
  4517.     my $status = shift;
  4518.     if ($status eq 'COMPLETE') {
  4519.     $o->inform(
  4520.         "Successfully removed $pkg version $version from $target_name.\n"
  4521.     )
  4522.     }
  4523.     else {
  4524.     $o->inform(<<END);
  4525. $SEP
  4526. Remove '$pkg' version $version from $target_name.
  4527. $SEP
  4528. END
  4529.     }
  4530. }
  4531.  
  4532. sub cb_install {
  4533.     my $o = shift;
  4534.     unshift @_, $o, 'install';
  4535.     &cb_status;
  4536. }
  4537.  
  4538. sub cb_upgrade {
  4539.     my $o = shift;
  4540.     unshift @_, $o, 'upgrade';
  4541.     &cb_status;
  4542. }
  4543.  
  4544. sub cb_status {
  4545.     my $o = shift;
  4546.     my $ACTION = shift;
  4547.     my $pkg = shift;
  4548.     my $version = shift;
  4549.     my $target_name = shift;
  4550.     my $status = shift;
  4551.     my $bytes = shift;
  4552.     my $total = shift;
  4553.     my $secs = shift;
  4554.  
  4555.     my $cols = $ENV{COLUMNS} || 78;
  4556.  
  4557.     $o->inform(<<END) and return if ($status eq 'PRE-INSTALL');
  4558. $SEP
  4559. \u$ACTION '$pkg' version $version in $target_name.
  4560. $SEP
  4561. END
  4562.  
  4563.     # Print the output on one line, repeatedly:
  4564.     my ($line, $pad, $eol);
  4565.     if ($status eq 'DOWNLOAD') {
  4566.     if ($bytes < $total) {
  4567.         $line = "Transferring data: $bytes/$total bytes.";
  4568.         $eol = "\r";
  4569.     }
  4570.     else {
  4571.         $line = "Downloaded $bytes bytes.";
  4572.         $eol = "\n";
  4573.     }
  4574.     }
  4575.     elsif ($status eq 'PRE-EXPAND') {
  4576.     $line = ""; #"Extracting package. This may take a few seconds.";
  4577.     $eol = "\r";  #"\n";
  4578.     }
  4579.     elsif ($status eq 'EXPAND') {
  4580.     $line = "Extracting $bytes/$total: $secs";
  4581.     $eol = $bytes < $total ? "\r" : "\n";
  4582.     }
  4583.     elsif ($status eq 'COMPLETE') {
  4584.     my $verb = $ACTION eq 'install' ? 'installed' : 'upgraded';
  4585.     $o->inform(
  4586.         "Successfully $verb $pkg version $version in $target_name.\n"
  4587.     );
  4588.     return;
  4589.     }
  4590.     $pad = ' ' x ($cols - length($line));
  4591.     $o->verbose($line, $pad, $eol);
  4592. }
  4593.  
  4594. sub warn_profile_change {
  4595.     my $o = shift;
  4596.     my $ok = shift;
  4597.  
  4598.     my $profile_track = $o->conf('profile-track');
  4599.     my $profile = PPM::UI::profile_get()->result;
  4600.  
  4601.     if ($profile_track) {
  4602.     $o->verbose(<<END);
  4603. Tracking changes to profile '$profile'.
  4604. END
  4605.     }
  4606. }
  4607.  
  4608. sub parse_range {
  4609.     my @numbers;
  4610.     my $arg;
  4611.     while ($arg = shift) {
  4612.       while ($arg) {
  4613.     if ($arg =~ s/^\s*,?\s*(\d+)\s*-\s*(\d+)//) {
  4614.         push @numbers, ($1 .. $2);
  4615.     }
  4616.     elsif ($arg =~ s/^\s*,?\s*(\d+)//) {
  4617.         push @numbers, $1;
  4618.     }
  4619.     else {
  4620.         last;
  4621.     }
  4622.       }
  4623.     }
  4624.     @numbers;
  4625. }
  4626.  
  4627. sub raw_args {
  4628.     my $o = shift;
  4629.     strip($o->line_args);
  4630. }
  4631.  
  4632. sub strip {
  4633.     my $f = shift;
  4634.     $f =~ s/^\s*//;
  4635.     $f =~ s/\s*$//;
  4636.     $f;
  4637. }
  4638.  
  4639. # matches("neil", "ne|il") => 1
  4640. # matches("ne", "ne|il") => 1
  4641. # matches("n", "ne|il") => 0
  4642. sub matches {
  4643.     my $cmd = shift;
  4644.     my $pat = shift || "";
  4645.  
  4646.     my ($required, $extra) = split '\|', $pat;
  4647.     $extra ||= "";
  4648.     my $regex = "$required(?:";
  4649.     for (my $i=1; $i<=length($extra); $i++) {
  4650.     $regex .= '|' . substr($extra, 0, $i);
  4651.     }
  4652.     $regex .= ")";
  4653.     return $cmd =~ /^$regex$/i;
  4654. }
  4655.  
  4656. sub pause_exit {
  4657.     my $o = shift;
  4658.     my $exit_code = shift || 0;
  4659.     my $pause = shift || 0;
  4660.     if ($pause) {
  4661.     if ($o->have_readkey) {
  4662.         $o->inform("Hit any key to exit...");
  4663.     }
  4664.     else {
  4665.         $o->inform("Hit <ENTER> to exit...");
  4666.     }
  4667.     $o->readkey;
  4668.     }
  4669.     exit $exit_code;
  4670. }
  4671.  
  4672. #============================================================================
  4673. # Check if this is the first time we've ever used profiles. This can be
  4674. # guessed: if the 'profile' entry is not set, but the 'profile-track' flag
  4675. # is, then it's the first time profile-track has been set to '1'.
  4676. #============================================================================
  4677. sub setup_profile {
  4678.     my $o = shift;
  4679.     $o->inform(<<END);
  4680. $SEP
  4681. You have profile tracking turned on: now it's time to choose a profile name.
  4682. ActiveState's PPM 3 Server will track which packages you have installed on
  4683. your machine. This information is stored in a "profile", located on the
  4684. server.
  4685.  
  4686. Here are some features of profiles:
  4687.  o You can have as many profiles as you want;
  4688.  o Each profile can track an unlimited number of packages;
  4689.  o PPM defaults to "tracking" your profile (it updates your profile every time
  4690.    you add or remove a package;
  4691.  o You can disable profile tracking by modifying the 'profile-track' option;
  4692.  o You can manually select, save, and restore profiles;
  4693.  o You can view your profile from ASPN as well as inside PPM 3.
  4694. $SEP
  4695.  
  4696. END
  4697.  
  4698.     my $response = PPM::UI::profile_list();
  4699.     my @l;
  4700.     unless ($response->ok) {
  4701.     $o->warn("Failed to enable profile tracking: ".$response->msg);
  4702.     $o->warn(<<END);
  4703.  
  4704. You can still use PPM3, but profiles are not enabled. To try setting up
  4705. profiles again, enter 'set profile-track=1'. Or, you can set up profiles
  4706. by hand, using the 'profile add' command.
  4707.  
  4708. END
  4709.     $o->run('unset', 'profile-track');
  4710.     return;
  4711.     }
  4712.     else {
  4713.     @l = sort $response->result_l;
  4714.     $o->inform("It looks like you have profiles on the server already.\n")
  4715.       if @l;
  4716.     $o->print_pairs([1 .. @l], \@l, '. ', 1, ' ');
  4717.     $o->inform("\n") if @l;
  4718.     }
  4719.  
  4720.     require PPM::Sysinfo;
  4721.     (my $suggest = PPM::Sysinfo::hostname()) =~ s/\..*$//;
  4722.     $suggest ||= "Default Profile";
  4723.     my $profile_name = $o->prompt(
  4724.     "What profile name would you like? [$suggest] ", $suggest, @l
  4725.     );
  4726.  
  4727.     my $select_existing = grep { $profile_name eq $_ } $response->result_l
  4728.       if $response->ok;
  4729.     if ($select_existing) {
  4730.     $o->inform("Selecting profile '$profile_name'...\n");
  4731.     PPM::UI::profile_set($profile_name);
  4732.     $o->inform(<<END);
  4733. You should probably run either 'profile save' or 'profile restore' to bring
  4734. the profile in sync with your computer.
  4735. END
  4736.     }
  4737.     elsif ($response->ok) {
  4738.     $o->inform("Creating profile '$profile_name'...\n");
  4739.     $o->run('profile', 'add', $profile_name);
  4740.     $o->inform("Saving profile '$profile_name'...\n");
  4741.     $o->run('profile', 'save');
  4742.     $o->inform(<<END);
  4743. Congratulations! PPM is now set up to track your profile.
  4744. END
  4745.     }
  4746.     else {
  4747.     $o->warn($response->msg);
  4748.     $o->warn(<<END);
  4749.  
  4750. You can still use PPM3, but profiles will not be enabled. To try setting up
  4751. profiles again, enter 'set profile-track=1'. Or, you can set up profiles
  4752. yourself using the 'profile add' command.
  4753.  
  4754. END
  4755.     $o->run('unset', 'profile-track');
  4756.     }
  4757. }
  4758.  
  4759. package main;
  4760. use Getopt::Long;
  4761. use Data::Dumper;
  4762.  
  4763. $ENV{PERL_READLINE_NOWARN} = "1";
  4764. $ENV{PERL_RL} = $^O eq 'MSWin32' ? "0" : "Perl";
  4765.  
  4766. my ($pause, $input_file, $target);
  4767.  
  4768. BEGIN {
  4769.     my ($shared_config_files, @fixpath, $gen_inst_key);
  4770.  
  4771.     Getopt::Long::Configure('pass_through');
  4772.     $target = 'auto';
  4773.     GetOptions(
  4774.     'file=s' => \$input_file,
  4775.     'shared' => \$shared_config_files,
  4776.     'target:s' => \$target,
  4777.     'fixpath=s' => \@fixpath,
  4778.     'generate-inst-key' => \$gen_inst_key,
  4779.     pause => \$pause,
  4780.     );
  4781.     Getopt::Long::Configure('no_pass_through');
  4782.  
  4783.     if ($shared_config_files) {
  4784.     $ENV{PPM3_shared_config} = 1;
  4785.     }
  4786.  
  4787.     if (@fixpath) {
  4788.     PPM::UI::target_fix_paths(@fixpath);
  4789.     exit;
  4790.     }
  4791.     if ($gen_inst_key) {
  4792.     require PPM::Config;
  4793.     PPM::Config::load_config_file('instkey');
  4794.     exit;
  4795.     }
  4796. }
  4797.  
  4798. # If we're being run from a file, tell Term::Shell about it:
  4799. if ($input_file) {
  4800.     my $line = 0;
  4801.     open SCRIPT, $input_file or die "$0: can't open $input_file: $!";
  4802.     my $shell = PPMShell->new(
  4803.     term => ['PPM3', \*SCRIPT, \*STDOUT],
  4804.     target => $target,
  4805.     pager => 'none',
  4806.     );
  4807.     $shell->setmode('SCRIPT');
  4808.     while (<SCRIPT>) {
  4809.     $line++;
  4810.     next if /^\s*#/ or /^\s*$/;
  4811.     my ($cmd, @args) = $shell->line_parsed($_);
  4812.     my $ret = $shell->run($cmd, @args);
  4813.     my $warn = <<END;
  4814. $0: $input_file:$line: fatal error: unknown or ambiguous command '$cmd'. 
  4815. END
  4816.     $shell->warn($warn) and $shell->pause_exit(2, $pause)
  4817.         unless $shell->{API}{cmd}{run}{found};
  4818.     $shell->pause_exit(1, $pause) unless $ret;
  4819.     }
  4820.     close SCRIPT;
  4821.     $shell->pause_exit(0, $pause);
  4822. }
  4823.  
  4824. # If we've been told what to do from the command-line, do it right away:
  4825. elsif (@ARGV) {
  4826.     my $shell = PPMShell->new(target => $target, pager => 'none');
  4827.     $shell->setmode('BATCH');
  4828.     my $ret = $shell->run($ARGV[0], @ARGV[1..$#ARGV]);
  4829.     my $warn = <<END;
  4830. Unknown or ambiguous command '$ARGV[0]'; type 'help' for commands.
  4831. END
  4832.     $shell->warn($warn) and $shell->pause_exit(2, $pause)
  4833.     unless $shell->{API}{cmd}{run}{found};
  4834.     $shell->pause_exit(0, $pause) if $ret;
  4835.     $shell->pause_exit(1, $pause);
  4836. }
  4837.  
  4838. # Just run the command loop
  4839. if (-t STDIN and -t STDOUT) {
  4840.     my $shell = PPMShell->new(target => $target);
  4841.     $shell->setmode('SHELL');
  4842.     $shell->cmdloop;
  4843. }
  4844. else {
  4845.     die <<END;
  4846.  
  4847. Error:
  4848.     PPM3 cannot be run in interactive shell mode unless both STDIN and
  4849.     STDOUT are connected to a terminal or console. If you want to
  4850.     capture the output of a command, use PPM3 in batch mode like this:
  4851.  
  4852.        ppm3 search IO-stringy > results.txt
  4853.  
  4854.     Type 'perldoc ppm3' for more information.
  4855.  
  4856. END
  4857. }
  4858.  
  4859.  
  4860. =head1 NAME
  4861.  
  4862. ppm3-bin - ppm3 executable
  4863.  
  4864. =head1 SYNOPSIS
  4865.  
  4866. Do not run I<ppm3-bin> manually. It is meant to be called by the wrapper
  4867. program I<ppm3>. See L<ppm3>.
  4868.  
  4869. =head1 DESCRIPTION
  4870.  
  4871. I<ppm3> runs I<ppm3-bin> after setting up a few environment variables. You
  4872. should run I<ppm3> instead.
  4873.  
  4874. For information about I<ppm3> commands, see L<ppm3>.
  4875.  
  4876. =head1 SEE ALSO
  4877.  
  4878. See L<ppm3>.
  4879.  
  4880. =head1 AUTHOR
  4881.  
  4882. ActiveState Corporation (support@ActiveState.com)
  4883.  
  4884. =head1 COPYRIGHT
  4885.  
  4886. Copyright (C) 2001, 2002, ActiveState Corporation. All Rights Reserved.
  4887.  
  4888. =cut
  4889.